Have you added this package?
Microsoft.AspNetCore.SignalR.Protocols.MessagePack
https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Protocols.MessagePack/3.0.0-preview9.19424.4
Also, to get to the UI thread, the best dispatcher to use is like this:
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
// Your UI update code goes here!
});
(from this SO question: Correct way to get the CoreDispatcher in a Windows Store app)
There's no magic to using MessagePack. The only advantage to using MessagePack is you might use less bandwidth than JSON text. However, it's harder to debug when you're trying to fix a problem.
I'd recommend you use JSON protocols while developing your app... then you can use Fiddler to read all of the JSON SignalR messages going back and forth between your server and app. Switch to MessagePack towards the end.
** Referring to code supplied in another answer **
I don't think you understand how to use async/await very well. In your page constructor, your StartAsync
method is actually asynchronous. Basically, this call will start your SignalR connection, but then keep going before the connection start has finished. This means checking the state of the connection is still going to be disconnected... and trying to send anything out on that connection is not going to work. You also can't await anything in a constructor. Instead, move most of this to the OnNavigatedTo
override method. You can leave the building of the connection on the constructor though.
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
await connection.StartAsync();
// etc...
base.OnNavigatedTo(e);
}
Finally, unless you have a good reason for doing so, never create an an asynchronous method that returns void
. If it doesn't return anything, then at least make it return Task
. That way, you can actually await
your method when you call it. A void
returning asynchronous method is a "fire-and-forget" method... which will cause you headaches if you can't anticipate the consequences.
public Task JoinGroupAsync()
{
return connection.InvokeAsync("JoinGroup", groupName);
}
Now, you can call this method like this: await JoinGroupAsync()
(The 'Async' on the end of the method name is just convention. It's not required.)