1

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.

6
  • Post the server-side code. Is your service running under the same account as your console app? Mar 4, 2014 at 3:43
  • I have added my server code Mar 4, 2014 at 5:35
  • It may not be the issue, but your OnStart code is wrong. OnStart is called on a thread that doesn't "belong" to you - it's wrong to block it with a Wait call. The Service Control manager doesn't consider your service to be started until you return from OnStart. If you want to have something running (or blocking) on a thread, you need to create one for yourself. Mar 4, 2014 at 7:18
  • So how should my OnStart code be? Mar 4, 2014 at 8:28
  • Typically you'd create your own threads and/or timers to do whatever work your service is meant to be doing and then return. Mar 4, 2014 at 9:19

1 Answer 1

3

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
2
  • Do take a look at my OP. I have updated it. It seems changing the code in my server worked.. but I'm not sure why Mar 4, 2014 at 5:36
  • Isn't that what I said in my answer? You got rid of the word localhost.
    – Bob Horn
    Mar 4, 2014 at 12:32

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.