-
Notifications
You must be signed in to change notification settings - Fork 1
Home
On the client with SignalR you often come into the problem that there are no real definitions for functions defined by a hub on the server in .NET.
What this project allows you to do is define an interface that represents the functions and events that a Server Hub has and then call into that interface like it was the hub. This way functions that require to call your Hubs are forced to use and expect the correct information at compile time, reducing errors.
The main limitation with this is you still require the Interface to be defined on the client, you can't get one off the server. While API's should always remain constant there is always the possibility of the Client and Server side definitions becoming out of Sync.
How to use the Strong Typed proxy is really simple
Install the NuGet Package https://www.nuget.org/packages/SignalRNetClientProxyMapper
Create an Interface that inherits IClientHubProxyBase
- Define Methods on the server like they would be on the server hub (You can even create an interface from the hub and directly copy it over)
- Define Subscribable Events with an IDisposable
public interface IChatHub : IClientHubProxyBase
{
Task SendChatMessage(string message);
IDisposable RecievedChatMessage(Action<string> action);
}Create your Connection and Proxy and voila:
If you don't want the internal HubProxy
IChatHub _stronglyTypedChatHub = _connection.CreateStrongHubProxy<IChatHub>(hubProxy);If you want to keep the internal HubProxy
IChatHub _stronglyTypedChatHub = null;
var hubProxy = _connection.CreateHubProxy("ChatHub");
_stronglyTypedChatHub = _stronglyTypedChatHub.GetStrongTypedClientProxy(hubProxy);Using the proxy is straight-forward and simple: A simple chat application using the above hub
_stronglyTypedChatHub.RecievedChatMessage(message => Console.WriteLine(message));
while(true){
_stronglyTypedChatHub.SendChatMessage(Console.ReadLine());
}The Client Proxy allows you to define custom names for functions just like on the server side:
[HubName("customServerHubName")]
public interface IChatHub : IClientHubProxyBase
{
[HubMethodName("customServerMethodName")]
Task SendChatMessage(string message);
IDisposable RecievedChatMessage(Action<string> action);
}We also allow you the ability of disabling mapping support for any member of your interface. The Mapper will not try to use these and they don't have to conform to any standard.
public interface IChatHub : IClientHubProxyBase
{
Task SendChatMessage(string message);
IDisposable RecievedChatMessage(Action<string> action);
[NotMapped]
void DoWork();
}