17
votes

I already know from asking here that I can have SignalR in a plain old Windows Service. I want to communicate between that Windows Service as a SignalR Client and a ASP.NET MVC SignalR Server. I used this hubs guide, It said

Server code that specifies the URL

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

If I do that, where will the messages go? What is the hub when you do new HubConfiguration?

It says I can connect with

NET client code that specifies the URL
 var hubConnection = new HubConnection("http://contoso.com/signalr", useDefaultUrl: false);

I'll have that in my Windows Server but if I send text to http://myserver.com/signalr on the server, how do I get it? What hub?

Also where best to put this in the Windows service? An example would be great!

4

4 Answers

19
votes

This solves the problem

On the client (Windows Service):

protected override async void OnStart(string[] args)
        {
            eventLog1.WriteEntry("In OnStart");
            try
            {
                var hubConnection = new HubConnection("http://www.savitassa.com/signalr", useDefaultUrl: false);
                IHubProxy alphaProxy = hubConnection.CreateHubProxy("AlphaHub");

                await hubConnection.Start();

                await alphaProxy.Invoke("Hello", "Message from Service");
            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry(ex.Message);
            }
        }

On the server:

public class AlphaHub : Hub
    {
        public void Hello(string message)
        {
            // We got the string from the Windows Service 
            // using SignalR. Now need to send to the clients
            Clients.All.addNewMessageToPage(message);

            // Send message to Windows Service

        }
2
votes

I think I don't understand exactly what you want.

  • Option A:

** Signal R. Server: Hosted as a Windows Service

** Signal R. Client: ASP.NET MVC Application.

  • Option B

** Signal R. Server: ASP.NET MVC Application.

** Signal R. Client: Windows Service

If what you need is Option A. You might want to take a look at "Signal R Self-Host Tutorial" If what you need is Option B. You need to create a .Net Signal R Client in the Windows Service. Please check out this tutorial on how to do so.

Regardless of your hosting type each hub has a unique name which you need to use when establishing the connection.

Regarding:

I'll have that in my Windows Server but if I send text to http://myserver.com/signalr on the server, how do I get it? What hub?

Signal R is an abstraction of realtime dual channel communication, so it really depends on the setup of your Hub.

Regarding:

Also where best to put this in the Windows service? An example would be great!

I would say, go simple and start by declaring a Singleton to start.

Hope it helps.

2
votes

Lets say you have the following code.

        var _connection = new HubConnection("http://localhost:61194/signalr");
        var _myHub = _connection.CreateHubProxy("MyHub");

You can create as many proxies that you have in the server. Then you can invoke the server method in following ways.

         _myHub.On<string>("recieved", data =>
            {
                System.Diagnostics.Debug.WriteLine("DATA : " + data);
            });
            _connection.Start().Wait();
            _myHub.Invoke("Hello").Wait();

You can put these code in the Windows Service method call which gets invoked from the timer or in the method which gets fired as per schedule.