Skip to content

MarkNickeson/SignalrCoreClientSideProxies

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Type-Safety Enhancements for SignalR Core Clients

Overview

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.*)

Basic Usage

Generating a proxy factory given server and client contracts

var proxyFactory = ClientSideProxies.GenerateFor<IServerContract, IClientContract>();

Creating and starting a hub connection

var hubConnection = new HubConnectionBuilder()
                            .WithUrl(...)
                            .Build();

await hubConnection.StartAsync();

Creating a strongly-typed client -> server helper

// 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);

Creating strongly-typed server -> client callback helper

// 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();

Info, Caveats & Limitations

  • 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();
    }

About

Strongly Typed Client Support for ASP.NET SignalR Core

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages