Ehsan Ghanbari

Experience, DotNet, Solutions

Communicate between Windows Service and SignalR Client

Today I had to make a connection between an old windows service and SignalR client. After tackling for an hour I decided to reference the Microsoft.AspNet.SignalR.Client library to my web service project and use it like below by overriding the OnStart method, actually I created the SignalR hub connection in the web service method:

 

 protected override async void OnStart(string[] args)
        {
               var hubConnection = new HubConnection("http://www.MySignalRDomain.com/signalr", useDefaultUrl: false);
               IHubProxy myProjectHub= hubConnection.CreateHubProxy("myProjectHub");

               await hubConnection.Start();

               await myProjectHub.Invoke("HiToYou", "an invoked message, for example");
         }

 

And in the SignalR service, I created the target hub to get the string ("an invoked message,  for example") from the web service in HiToYou method :

 
   

 public class myProjectHub: Hub
    {
       public void HiToYou(string message)
        {
                  Clients.All.addNewMessageToPage(message);

         }

       }

 

Needless to say, if you are using OwinStartup, you need to register SignalR middleware:

 

 

using Microsoft.Owin;

using Owin;

 

[assembly: OwinStartup(typeof(MyApplication.Startup))]

namespace MyApplication

{

    public class Startup

    {

        public void Configuration(IAppBuilder app)

        {                  

            app.MapSignalR("/signalr", new HubConfiguration());

        }

    }

}

 

Hope it would be useful!



About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
using Log4net in asp.net MVC4
How to use PagedList In asp.net MVC
Redis as a cache server
Domain driven design VS model driven architecture
What's the DDD-lite?
Multiple submit buttons in asp.net MVC