Click here to Skip to main content
15,943,008 members
Articles / Programming Languages / C#

SignalR with Silverlight 5

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
11 Jul 2013CPOL2 min read 30.2K   12   3
SignalR with Silverlight 5.

SignalR is truly an amazing API. It’s ridiculously easy to use. The majority of SignalR client examples that I see on the web are for HTML/JS or WPF. There are examples for Silverlight but for older version of SignalR and Silverlight. What I was hoping to find was a step by step example using VS2k12, SL5 and latest greatest Nuget Packages. Since I could not find one, I thought I would create one and share. This step by step was highly inspired by the samples found here:

Setting up the Solution

Recommend you download the code and follow along. In Visual Studio 2012, create a new Silverlight 5 application called “SilverlightClientSignalR” as seen below.



This will create 2 projects, SilverlightClientSignalR.Web and SilverlightClientSignalR. Add the following Nuget packages to each project.

Project NameNuget Package
SilverlightClientSignalR1. Microsoft.ASP.NET SignalR .NET Client

2. Async for .NET Framework 4, Silverlight 4 and 5…


Add a new ASP.NET MVC 4 Web Application. I used the Basic template.



SignalR Server Preparation

Add a new folder to the HubWeb project called “Hubs”. Inside that folder, add a new item and choose SignalR Hub Class as seen below. Name it TestHub.cs.


Nice feature of adding a new SignalR Hub class is the necessary SignalR packages are automatically added to your references as well. Modify the code to look like this:


We need to tell the ASP.NET routing system how to find the hubs. In Global.asax.cs, add the following code:


Since we need a Silverlight client to be able to call into the SignalR Hub service, we need to add a clientaccesspolicy.xml file to the HubWeb project to enable cross domain communication.

At this point, if you run the solution and type in http://localhost:{yourport#}/signalr/hubs, you should see the automatically created hubs.js file.

Silverlight Client SignalR Preparation

First, let’s add a button and TextBox named Messages to MainPage.xaml as seen below.


In the code behind for MainPage.xaml.cs, add the following code:


Add SignalRClient helper class to the Silverlight project.


The RunAsync method is the key to connecting your Silverlight client to the SignalR server. The minimum requirements to hook up a Silverlight client is to:

  1. Instantiate a HubConnection passing in the URL of the SignalR server and calling Start.
  2. For each server Hub, create a HubProxy to be able to receive and call messages.

Running the Code

Download the code here. Run the code and you should see the MainPage Silverlight page receiving messages from the SignalR server. To see the value of SignalR, copy and paste the URL into multiple tabs across multiple browsers. Take note that when you push a button, all the other tabs receive the message in near time. Silverlight clients talking to each other in real time, pretty cool eh?



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Chief Technology Officer PROMODEL Corporation
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

BugAm getting error in test project: The connection has not been established. Pin
Gregoryagu21-Sep-13 7:45
Gregoryagu21-Sep-13 7:45 
GeneralRe: Am getting error in test project: The connection has not been established. Pin
Guga_x2-Oct-13 15:52
Guga_x2-Oct-13 15:52 
GeneralRe: Am getting error in test project: The connection has not been established. Pin
CCB201018-Nov-13 6:02
CCB201018-Nov-13 6:02 
I'm having trouble getting this running, too.
I changed the HubWeb project to use Local IIS, and VS2013 set that up. In IIS Manager I see HubWeb under "Default Web Site".

In RunAsync() a SecurityException is thrown here:
await _hubConnection.Start();

Are there any more detailed setup/configuration instructions available?

modified 18-Nov-13 12:26pm.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.