SignalR Cannot call type without delegate

I'm trying to learn SignalR by writing a really simple application ... it basically sends "Hello" periodically (like Stock Ticker , but much simpler).

Here's my hub:

public class StockTickerHub : Hub
{
    public void Hello()
    {
        var s = StockTicker.stockTicker;

        Clients.All.hello();
    }
}

      

... and here's the code that should send messages periodically:

public class StockTicker
{
    public static StockTicker stockTicker = new StockTicker();
    private Thread thread;

    public StockTicker()
    {
        var stockTickerHub = GlobalHost.ConnectionManager.GetHubContext<StockTickerHub>();

        this.thread = new Thread(() =>
            {
                while (true)
                {
                    stockTickerHub.Clients.All().hello();
                    Thread.Sleep(1000);
                }
            }
        );

        this.thread.Start();
    }
}

      

I am getting RuntimeBinderException

in stockTickerHub.Clients.All().hello();

. It says:

An unhandled exception of type "Microsoft.CSharp.RuntimeBinder.RuntimeBinderException" occurred in System.Core.dll

More info: Cannot call type without delegate

What am I doing wrong?

Client-side JavaScript below, just in case you need to replicate this.

<script type="text/javascript">
    $(function () {

        var chat = $.connection.stockTickerHub;

        chat.client.hello = function () {
            $("#log").append("Hello");
        }

        $.connection.hub.start().done(function () {

            chat.server.hello();
        });
    });
</script>

      

+3


source to share


1 answer


Just change:

stockTickerHub.Clients.All().hello();

      

in

stockTickerHub.Clients.All.hello();

      



The debugger should already report this error. I tried your code after updating. He works.

A note about code design:

I would not start a new dispatch thread in an event hello

that would fire every time this method is called by any client. I don't think what you want to do. As a smoother example, you can run a ticker in a classroom Startup

. (If you want the per-connection ticker to override OnConnected

, get the client's connection id and give them separate tickers ...)

+10


source







All Articles