Search code examples
javascript.netasp.net-mvcsignalrsignalr-hub

SignalR sharing connection between 2 apps


I have 2 projects in a solution, one is MVC app and the second one is simple Windows Forms app. What I'm currently trying to do, for testing purposes, is to control content on my MVC app through Windows Forms app. To simplify this further, I have a button on my app, which should, when clicked, update HTML on my connected clients. So MVC should manage client connections, and Forms app should be dedicated to content administration.

What I did is this - MVC hub setup

public class ConnectionHub : Hub
{
    public void Update()
    {
        Clients.All.updatesequence(" ");
    }
}

MVC Startup setup:

[assembly: OwinStartup(typeof(Web.Startup))]

namespace Web
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {      
            GlobalHost.DependencyResolver.UseSqlServer(
        ConfigurationManager.ConnectionStrings["signalr"].ConnectionString);
            app.MapSignalR();
        }
    }
}

Forms app - on button clicked event:

 private void button1_Click(object sender, EventArgs e)
 {
        var context = GlobalHost.ConnectionManager.GetHubContext<ConnectionHub>();
        context.Clients.All.updatesequence(" ");
 }

Javascript client function "updatesequence()" should be ok because I tested it in different setups and it worked. Database connection also works.

EDIT: my question is - how to make this work? It doesn't work in current setup. Context in Forms is loaded and "updatesequence" method is called, but nothing happens.


Solution

  • You are calling server-side code in your Winforms app, which is not right. In your Winforms app you should reference the SignalR .NET client library, and with that one connect to your server and call methods into your hub (or maybe another one you might want to introduce for admin purposes).

    Have a look at this: http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-net-client

    Also, unless you are planning to have multiple server and you need horizontal scalability, which at this stage is not probably your main concern, you do not need the Sql Server backplane at all (call to UseSqlServer in your Startup class).