Regarding hosting SignalR in console apps

2019-05-13 16:43发布

问题:

i got a code which host SignalR in console apps. here is the code.

Install-Package Microsoft.Owin.Hosting -pre
Install-Package Microsoft.Owin.Host.HttpListener -pre
Install-Package Microsoft.AspNet.SignalR.Owin -pre

using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;

namespace SignalR.Hosting.Self.Samples
{
class Program
{
    static void Main(string[] args)
    {
        string url = "http://172.0.0.01:8080";

        using (WebApplication.Start<Startup>(url))
        {
            Console.WriteLine("Server running on {0}", url);
            Console.ReadLine();
        }
    }
}

class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // This will map out to http://localhost:8080/signalr by default
        // This means a difference in the client connection.

        app.MapHubs();
    }
}

public class MyHub : Hub
{
    public void Send(string message)
    {
        Clients.All.addMessage(message);
    }
}

}

i just do not understand this line using (WebApplication.Start<Startup>(url)) i just also do not understand the usage of Startup class

anyone can help me to understand the above code. thanks

回答1:

The Startup class shown here is where you configure SignalR; in this case it is using a basic approach that is just going to find all the hubs (the Hub subclasses in the calling assembly) and throw them into the mix by-name - but more subtle configurations are possible. The WebApplication.Start<Startup>(url) is invoking all that configuration code, along with the plumbing to get a listener, etc to do some actual work. Ultimately it is the Hub that has the interesting code here, i.e. where your code goes.



标签: c# signalr