A little introduction of ASP.NET Core SignalR and How to connect it using official .Net Standard 2.0 C# client library in Xamarin.Forms project for Front-end Developer.
1. ASP.NET Core SignalR &
Xamarin.Forms
Connect ASP.NET Core SignalR service via .NET Standard 2.0 C#
client library in Xamarin.Forms
2. ASP.NET Core SignalR
• A re-designed SignalR(http://signalr.net/) library for ASP.NET
Core 2.1 to provide real-time communication:
http://github.com/aspnet/signalr
• Not compatible with old ASP.NET SignalR.
• Existing Application that use ASP.NET SignalR or ASP.NET Core
SignalR:
• Office 365 real-time document co-authoring: http://bit.ly/2KIZjrb
• Visual Studio Live Share: http://bit.ly/2Nl8Aak
3. ASP.NET Core SignalR Features
• Multiple transport mechanism:
• Websocket: http://bit.ly/2MLVwtL
• Server-Send event: http://bit.ly/2KuvkXW
• Long Polling: http://bit.ly/2lOvg6t
• Hub concepts for client & server communication:
• Hub protocols: text based JSON & binary based MessagePack (
http://msgpack.org/ )
• Support 3 RPC style:
1. Server RPC call
2. Server Streaming RPC call
3. Server Call Client defined function
4. ASP.NET Core SignalR Features
• Hub Protocol spec is opened:
http://github.com/aspnet/SignalR/blob/master/specs/HubProtocol.m
d
• Provide many language implementation of client library:
• JavaScript: http://www.npmjs.com/package/@aspnet/signalr
• C#: http://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Client/
• C++ & Java Client are under development and will release stable version with
ASP.NET Core 2.2 timeline.
• 3rd Party:
• Swift : http://github.com/moozzyk/SignalR-Client-Swift
• RxJs enabled Js client: http:///www.npmjs.com/package/@ssv/signalr-client
5. ASP.NET Core SignalR Features
• Hub support ASP.NET Core Authentication mechanism
• Cookie
• JWT Auth Token
• Service instance can be scale-out using Redis on-premises,
using Azure SignalR Service(https://aka.ms/signalr_service ) in
cloud.
• Note: ASP.NET SignalR can scale-out via Azure Service Bus, but
there’s no plan for ASP.NET Core SignalR right now:
https://github.com/aspnet/SignalR/issues/1676
6. Xamarin & ASP.NET Core SignalR
• Using ASP.NET Core SignalR technology, we can build a cross
platform/device broadcasting system via Xamarin.
• Demo code:
http://bit.ly/2KHv57D
7. Xamarin.Forms as Core SignalR client
Using following Nuget Package in Xamarin.Forms project:
• Microsoft.AspNetCore.SignalR.Client:
https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Client/
• Microsoft.Extensions.Logging.Console:
(for iOS & Android logging)
https://www.nuget.org/packages/Microsoft.Extensions.Logging.Console/
• Microsoft.Extensions.Logging.Debug:
(for UWP logging)
https://www.nuget.org/packages/Microsoft.Extensions.Logging.Debug/
• System.Threading.Channels:
(for invoke Server Streaming RPC call)
https://www.nuget.org/packages/System.Threading.Channels/
8. Xamarin.Forms as Core SignalR client
• Hub: a connection joint that client & sever can call their defined
methods mutually.
Server
Client #1
Client #2
Client #3
Hub
9. Xamarin.Forms as Core SignalR client
• On Client side, use “HubConnection(http://bit.ly/2tRduE6)” to
access the Server Method defined in Hub.
• To create HubConnection object, using
“HubConnectionBuilder(http://bit.ly/2MLLGYC)” to create.
• HubConnectionBuilder has many Fluent-API style extension
method to configure it. (ASP.NET Core configuration style)
10.
11. Xamarin.Forms as Core SignalR client
• This kind of “Code as Configuration” coding style is very
modifiable based on demand, and can be refactored to a clear
style using Action Delegate:
http://www.tutorialsteacher.com/csharp/csharp-action-delegate
12. Xamarin.Forms as Core SignalR client
• Once HubConneciton instance is created, call StartAsync() to
connect to remote ASP.NET Core SignalR server, call
StopAsync() or DisposeAsync() to tear down connection,
HubConnection object cannot be re-used, must create it every
time when starting a new connection.
13. Xamarin.Forms as Core SignalR client
• Server RPC call : (http://bit.ly/2KKDXN8)
• Use HubConnectionExtensions.InvokeAsync<TResult>()
extension method(http://bit.ly/2MOpy03 ) to get RPC call result.
14. Xamarin.Forms as Core SignalR client
• Server Streaming RPC call: http://bit.ly/2tQLbp4
• Use
HubConnectionExtensions.StreamAsChannelAsync<TResult>()
extension method (http://bit.ly/2zfC1bc),
• Which needs additional New Type called
“ChannelReader(http://bit.ly/2MJARXi)” from .NET Core runtime, that
is not documented in official docs yet.
Channel(http://bit.ly/2KJAkXG) is a kind of Data Queue that
mimics Go’s “Buffered Channel(http://bit.ly/2IRo56K)” concept:
https://github.com/stephentoub/corefxlab/blob/master/src/Syst
em.Threading.Tasks.Channels/README.md
15. Xamarin.Forms as Core SignalR client
• Server Streaming RPC call can be cancelled from client via Set
Cancellation token parameter in
HubConnectionExtensions.StreamAsChannelAsync<TResult>()
16. Xamarin.Forms as Core SignalR client
• Server Call Client defined function: (http://bit.ly/2lT4cDf)
Register client function using HubConnection.On() or
HubConnectionExtensions.On(http://bit.ly/2IQ0Auw) API:
17. Other Notes
Xamarin Client need to use latest preview of Visual Studio 2017 15.8
preview 3 or Visual Studio for Mac 7.6 preview 3:
https://github.com/aspnet/Announcements/issues/305
• UWP on Windows 10 Mobile OS is not support since .NET Standard
2.0 support for Windows 10 UWP comes in 2017 Fall Creators
Update.
• UWP logging has to use AddDebug(), not AddConsole().
• JWT Authentication is configured on HubConnectionBuilder’s
WithUrl() extension method:
https://github.com/aspnet/SignalR/blob/948ebf34ece11918804f443b65c6a053dfc
8f35c/samples/JwtClientSample/Program.cs#L38
• Local debugging & testing can use ngrok(http://ngrok.com) for real
device connection.