Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR Between MVC SignalR Server and Windows Service SIgnalR Client

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!

like image 784
user2471435 Avatar asked Feb 06 '14 20:02

user2471435


People also ask

Is SignalR two way communication?

There is a library called SignalR, which is included in ASP.NET Core. SignalR doesn't only enable you to achieve real-time two-way communication between applications. It also substantially simplifies the process of enabling all of this in the code.

What platform does SignalR client support?

Windows Desktop and Silverlight Applications In addition to running in a web browser, SignalR can be hosted in standalone Windows client or Silverlight applications.

Is SignalR backwards compatible?

The answer is no, SignalR is not backwards kompatible.

How many SignalR connections can a server handle?

IIS on client operating systems has a limit of 10 concurrent connections. SignalR's connections are: Transient and frequently re-established. Not disposed immediately when no longer used.


2 Answers

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

        }
like image 147
user2471435 Avatar answered Sep 28 '22 08:09

user2471435


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.

like image 44
wacdany Avatar answered Sep 28 '22 08:09

wacdany