简体   繁体   中英

SignalR .NET client works in console app but not in a windows service

Here's my SignalR console app which works fine. It's able to receive messages from the Hub and print them onto the console.

    static void Main(string[] args)
    {

        MainAsync().Wait();
        Console.ReadLine();
    }

    static async Task MainAsync()
    {
        try
        {

            var hubConnection = new HubConnection("http://localhost:8080/");
            IHubProxy hubProxy = hubConnection.CreateHubProxy("MyHub");
            hubProxy.On<string, string>("addMessage", (name, message) =>
            {
                Console.WriteLine("Incoming data: {0} {1}", name, message);
            });
            ServicePointManager.DefaultConnectionLimit = 10;
            await hubConnection.Start();

        }
        catch (Exception ex)
        {

        }
    }

I created a windows service, and copied almost character for character, except the part where it prints to the console, I printed it to a log file

    protected override void OnStart(string[] args)
    {
            Log.Info("Starting...");
            StartListening().Wait();
    }

    private static async Task StartListening()
    {
        try
        {
            var hubConnection = new HubConnection("http://localhost:8080/");
            IHubProxy hubProxy = hubConnection.CreateHubProxy("MyHub");
            hubProxy.On<string, string>("addMessage", (name, message) =>
            {
                Log.Info(string.Format("Incoming data: {0} {1}", name, message));
            });
            ServicePointManager.DefaultConnectionLimit = 10;
            await hubConnection.Start();
            Log.Info("Connected");
        }
        catch (Exception ex)
        {
            Log.Error(ex);
        }
    }

Here's my server code:

    static void Main(string[] args)
    {
        //string url = "http://localhost:8080";
        string url = "http://*:8080";
        using (WebApp.Start(url))
        {
            MyHub hub = new MyHub();
            Console.WriteLine("Server running on {0}", url);
            var key = Console.ReadLine();
            while (key != "quit")
            {
                hub.Send("Server", key);
            }
        }
    }

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR();
    }
}

public class MyHub : Hub
{
    public void Send(string name, string message)
    {
        //Clients.All.addMessage(name, message);
        var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
        context.Clients.All.addMessage(name, message);
    }
}

However, nothing gets written to the log file except "Starting..." and "Connected"

There were no exceptions thrown either.

Do I need to include anything else or modify the code for SignalR to work on a windows service?

Update

Apparently, it seemed that changing this line

string url = "http://localhost:8080";

to this

string url = "http://*:8080";

in my server code solved my issue. But I don't know why.

I had a similar issue. The problem was using localhost in the URL. Replace localhost with the actual server name.

The issue is explained here .

The URL is used to open an HttpListener. With the URL above (the localhost one) this HttpListener will accept requests from http://localhost only. Other, equivalent URLs do not work:

http://127.0.0.1
http://110.120.130.140
http://myserver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM