Blog

Filter posts by Category Or Tag of the Blog section!

Communicate between Windows Service and SignalR Client

Friday, 26 May 2017

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!

Category: Software

Tags: SignalR

comments powered by Disqus