Using SignalR with Unity

Introduction


ASP.NET SignalR is a new library for ASP.NET developers that makes developing real-time web functionality easy. SignalR allows bi-directional communication between server and client. Servers can now push content to connected clients instantly as it becomes available. Read more

I will demonstrate how to implement dependency injection with Unity IoC container in a SignalR project. We will be building a simple online chatting app.


Installing Packages


To begin your journey to the world of realtime web apps, start by installing these packages:

  • Unity IoC for DI
    • Install-Package Unity
  • Sever and client package for hosting with IIS and ASP.NET and Javascript client
    • Install-Package Microsoft.AspNet.SignalR
  • Server package for hosting SignalR endpoints
    • Install-Package Microsoft.AspNet.SignalR.Core
  • Client package .NET SignalR applications
    • Install-Package Microsoft.AspNet.SignalR.Client
  • Client package for javascript SignalR applications
    • Install-Package Microsoft.AspNet.SignalR.JS

Creating The Chat Messenger App


Part 1:
Create the UnityHubActivator class that implements IHubActivator interface.



Part 2:
Register your Hub class to the UnityConfiguration class. Create the UnityConfiguration class If you don't already have this class in the App_Start folder.























Notice the last line of code that is highlighted. Don't forget to add it.



Part 3:
Building the IRepos Interface and Repos class to handle chat messages.



















Part 4:
Creating your hub class




Finally!!! Add your Javascript client class to handle realtime messaging.




The hub class is now injected to the unity IoC to support dependency Injection which has totally improved the performance of your code and it also speed up things. If you have any issues, raise comments and if you have a better implementation, i am all ears. You can open up Google chrome and IE to see how the messages are updating in realtime.

Fore beginners, i suggest you read this book by Einar Ingebrigtsen or learn SignalR on the ASP.NET website.

This is just the beginning, more stuffs will be coming your way!!! 😊

Comments

Popular posts from this blog

Solved: Jwt Authentication in Asp.Net Web Api And Mvc

Sending Notification Message with Firebase with JavaScript and .Net client