Using SignalR with .Net client

29 בDecember 2012


Using SignalR with .Net client


In my previous post I demonstrated how to use SignalR with javascript client.
After this post, I got an email from my extremely talented colleague SignalR logo
Bnaya Eshet  asking if there’s a way to to SignalR with .Net client.
I said “yes” of course so now I want to demonstrate how to do it.
The server side code is the same as in my previous post, this is just
another client example.


Step 1: Create .Net client project:

Add some .Net project, any project type will do, I will demonstrate with 
a WPF project works with MVVM. The code samples will be from the

Step 2: Install SignalR .Net client nuget package

On the project you’ve just created, install Microsoft.AspNet.SignalR.Client
package.Make sure that both server project described in previous post has
the same version. If you didn’t, you will get an error of 
incompatible protocol version“ while trying to connect.

Step 3: Create SignalR connection instance

Using the namespace of Microsoft.AspNet.SignalR.Client.Hubs on
Microsoft.AspNet.SignalR.Client assembly (referenced to your project after
NuGet package installation) has a HubConnection class. This class used for
keeping persistent connection.

That’s how you create an instance of this class:

_connection = new HubConnection("http://localhost/RanWahle.Blog.SignalR.Demo");

           //useDefaultUrl = true

Where the first parameter is the site address, and the second  parameter

named useDefaultUrl tells you whether to use the default hubs URL or not.

You should set this parameter according to the way your hubs are registered

on your server.

The default value for that parameter is true, you need this one only if

the hubs aren’t registered at the default URL (thanks @davidfowl)



Step 4: Create proxy hub

Another component you can work with is the hub proxy component.

This componentis created by the connection in previous step, using its

CreateHubProxy method. This method returns an IHubProxy interface.

by default it will create a HubProxy class that implements it.

That’s how hub creation looks like:

IHubProxy proxy = connection.CreateHubProxy("soccerResultsHub");


Step 5: Handle server messages

The proxy created on previous step now needs to handle server messages. as seen in

my previous post The server calls Client.All.someMethod  pseudo method where

someMethod is actually a message that is sent to the clients. There are several

extension method of the IHubProxy interface, the recommended on is the On method

which accepts the event name as string, and the arguments as a collection of parameters.

This is how it looks like:

proxy.On<string, string>("updateTeams", (homeTeam, visitorTeam) =>


                          HomeTeamName = homeTeam;

                          VisitorsTeamName = visitorTeam;


We’ve used here two strings as a type parameters because the server sends two strings to the client

Here’s the corresponding server code.

Clients.All.updateTeams(_game.HomeTeam, _game.VisitorsTeam);


Step 6: Invoke server methods

In here, we use another IHubProxy method (a non extension one this time) called Invoke.

This method accepts the server side method as a string and a collection of parameters.

This is how it looks like:

proxy.Invoke("UpdateHomeTeamName", HomeTeamName)

Where the corresponding server method named UpdateHomeTeamName and accepts

one string as a parameter.



SignalR has a library for .Net client as well as javascript clients. If you are a .Net client

programmer you can also interact with the server suing SignalR . The steps above

demonstrate how you can do this very easily.

The full code example can be found here 

Enjoy, I’m sure you will.

kick it on

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



  1. Quinton18 בJanuary 2013 ב 0:29

    Thanks for this, I’ve been wanting to try it out in a non javascript client.

  2. Everhart21 בJanuary 2013 ב 22:35

    Highly energetic blog, I loved that bit. Will there be a part 2?

  3. evil22 בFebruary 2013 ב 3:25

    Thanks for the post!

    I am on signalR 1.0 final and in order to have a self hosting server I had to use preRelease Owin.
    I think this is the reason why I get incompatible protocol between client and server, but I don’t know how to solve this.
    Any clue?

  4. Ran Wahle26 בFebruary 2013 ב 7:17

    Evil hi,

    I’ve tried to reproduce your scenario, and succeeded.

    Incorrect protocol version is usually because of using two different versions of signalR, when RC2 signalR uses1.1 value as its protocol version, the release version using 1.2.

    Now, you can update your .Net client to have the release version and you’ll be able to connect.