Using SignalR in ASP.NET 5

January 29, 2015 by Anuraj

.Net ASP.Net ASP.Net MVC Visual Studio

In this post I am implementing a whiteboard solution using SignalR and ASP.NET 5.

To use SignalR, you need to add “Microsoft.AspNet.SignalR.Server” reference in the project.json file.

{
    "dependencies": {
        "Microsoft.AspNet.Diagnostics": "1.0.0-beta1",
        "Microsoft.AspNet.Hosting": "1.0.0-beta1",
        "Microsoft.AspNet.Server.WebListener": "1.0.0-beta1",
        "Microsoft.AspNet.StaticFiles": "1.0.0-beta1",
		"Microsoft.AspNet.SignalR.Server": "3.0.0-beta1"
    },
    "commands": {
        "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5010"
    },
    "frameworks": {
		"aspnet50": {},
        "aspnetcore50": {}
	}
}

Also you need Microsoft.AspNet.StaticFiles reference, as you need to serve Javascript and HTML files.

Now you need to modify the startup.cs file to use SignalR, similar to MVC, Microsoft introduced some extension methods to enable support for SignalR.

public class Startup
{
	public void Configure(IApplicationBuilder app)
	{
		app.UseServices(services =>
		{
			services.AddSignalR();
		});
		app.UseFileServer();
		app.UseSignalR();
	}
}

And you are ready to run the SignalR services. I have modified the script file to support SignalR client side changes. You can find the source code on github.

Happy Programming :)

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub