SignalR Self Hosting With .NET Client

If you have not heard of SignalR, you can think of it as a kind of RPC (remote procedure call), but it is much more advanced than that, where clients may be called by server code, and clients may call server code.


Here is what the SignalR web site has to say about itself:


ASP.NET SignalR is a new library for ASP.NET developers that makes it incredibly simple to add real-time web functionality to your applications. What is "real-time web" functionality? It’s the ability to have your server-side code push content to the connected clients as it happens, in real-time.

You may have heard of WebSockets, a new HTML5 API that enables bi-directional communication between the browser and server. SignalR will use WebSockets under the covers when it’s available, and gracefully fallback to other techniques and technologies when it isn’t, while your application code stays the same.

SignalR also provides a very simple, high-level API for doing server to client RPC (call JavaScript functions in your clients’ browsers from server-side .NET code) in your ASP.NET application, as well as adding useful hooks for connection management, e.g. connect/disconnect events, grouping connections, authorization.



I first wrote about SignalR a while back when I wrote this article with my partner in crime Richard King (Kingo to those that know him) :


At the time SignalR was very novel (even  though that article was only written a year ago) and the only option at that time was a JavaScript client with a ASP .NET hosted Hub. Things have obviously come on leaps and bounds since then. Now there are all sorts of options for using SignalR, here are some options available to you now

  • .NET client (OWIN) client (OWIN was new to me, here is what it means : Open Web Interface for .NET which you can read about more here : OWIN)
  • Azure client
  • Javascript client
  • Redis client

There is also the possibility to self host the Hub which was of particular interest to me for a new side project I wanted to work on. To this end I decided to give the following scenario a go:


  • A simple echo hub that would receive messages as strings, which would be reversed and sent to ALL connected clients. Now the clients could be .NET or JavaScript or any of the other supported hubs. For my requirements .NET was all I cared about.
  • A .NET client that would send messages to the server (via the console standard input), and would print out any messages from the server (via the console standard output)


The reason I decided to give this a go is that I could not find a single source which had a running demo example of self hosting, and using .NET as a client. So I hope this helps someone.


So without further ado, lets look at some code



The most relevant parts of the server are shown below:


Program (main entry point)

public class Program
    static void Main(string[] args)
        using (WebApplication.Start<Startup>("http://localhost:8080/"))
            Console.WriteLine("Server running at http://localhost:8080/");



This is the code that will initiate a connection

public class Startup
    public void Configuration(IAppBuilder app)
        app.MapConnection<RawConnection>("/raw", new ConnectionConfiguration { EnableCrossDomain = true });


The Hub

This is what clients will talk to

public class MessageRecievingHub : Hub
    public void SendMessage(string message)
        string it = new string(message.Reverse().ToArray());



So how about the client, well that is as simple as this:

class Program
    static void Main(string[] args)
        var hubConnection = new HubConnection("http://localhost:8080/");
        var serverHub = hubConnection.CreateHubProxy("MessageRecievingHub");
        serverHub.On("broadCastToClients", message => System.Console.WriteLine(message));
        string line = null;
        while ((line = System.Console.ReadLine()) != null)
            // Send a message to the server
            serverHub.Invoke("SendMessage", line).Wait();



As always here is a small demo project : https://dl.dropbox.com/u/2600965/SignalRClientServer.zip


WCF And the : “Your process does not have access rights to“ PITA



No matter how many times I create a new WCF service, I seem to always end up with this error.  I know the answer its to do with the UAC in machines Vista and above, and there are various tools to fix this. And there is also some web site that you used to get redirected to. For anyone else that has this issue running a Command Line as Administrator and running a command line similar to this should fix the issue


netsh http add urlacl url=http://+:PORT/WCF_SERVICE_NAME/ user=MACHINE\USERNAME



Hope that helps someone.


PS : for some free tools that do the same, have a look here: