SignalR Core is awesome! A current limitation, however, is that .NET clients lack compile-time type-safety.
This project enables the following type-safety enhancements for SignalR Core clients
- Automatic, runtime generation of:
- strongly-typed proxies for Client -> Server calls
- strongly-typed mapper proxies for Server -> Client callback mapping
- Runtime proxy factory generation based on Roslyn (Microsoft.CodeAnalysis.*)
var proxyFactory = ClientSideProxies.GenerateFor<IServerContract, IClientContract>();
var hubConnection = new HubConnectionBuilder()
.WithUrl(...)
.Build();
await hubConnection.StartAsync();
// use proxyFactory and hubConnection to create a server proxy
var clientToServerProxy = proxyFactory.CreateServerProxy(hubConnection);
// invoke server-side hub methods via strongly-typed client proxy
var rval = await clientToServerProxy.SampleMethod("A String",DateTime.Now);
// create your client callback implementation
IClientContract clientImpl = new MyClientCallbacks();
// use proxyFactory to create a client mapper proxy
var callbackProxy = proxyFactory.CreateClientMapperProxy(hubConnection, client);
// at this point server-side callbacks via HubContext.Clients.* will route
// automatically to clientImpl
// to "unsubscribe", dispose of callbackProxy
callbackProxy.Dispose();
-
Based on .NET Core 3.0 preview 6
-
Developed with Visual Studio 2019 Community Preview (Version 16.2.0 Preview 3.0)
-
Only async/await enabled contracts are supported
public interface IServer { Task ImOk(); Task<int> ImOkToo(); // int ImNotSupported(); // void MeEither(); } public interface IClient { Task ImOk(); // Task<int> ImOkToo(); // client callback return values not supported by SignalR // int ImNotSupported(); // void MeEither(); }