SignalR Self Host Disconnect TimeOut Without Globa

2019-07-24 10:44发布

问题:

I'm using SignalR 2.0 self-hosted with OWIN. I am trying to modify the SignalR ConnectionTimeout property, but it does not seem to work with the OWIN Startup class.

Current Attempt

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

namespace Test
{
    internal class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(10);
            app.UseCors(CorsOptions.AllowAll);
            app.MapSignalR();
        }
    }
}

I have also tried to just add my own Global.asax file copied from another project. However, I can't seem to resolve the System.Web.HttpApplication extension.

using System;
using Microsoft.AspNet.SignalR;

namespace Test
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(10);
        }
    }
}

Any suggestions how to tackle this?

Link to docs: SignalR Configuration

回答1:

You need to call GlobalHost before startup is called. Basically, if your code looks like this:

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

namespace SignalRSelfHost
{
class Program
{
    static void Main(string[] args)
    {
        // This will *ONLY* bind to localhost, if you want to bind to all addresses
        // use http://*:8080 to bind to all addresses. 
        // See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx 
        // for more information.
        string url = "http://localhost:8080";
        using (WebApp.Start(url))
        {
            Console.WriteLine("Server running on {0}", url);
            Console.ReadLine();
        }
    }
}
class Startup
{
    public void Configuration(IAppBuilder app)
    {
        GlobalHost.Configuration.ConnectionTimeout  =   new TimeSpan(0, 0, 110);
        GlobalHost.Configuration.DisconnectTimeout  =   new TimeSpan(0, 0, 60);
        GlobalHost.Configuration.KeepAlive          =   new TimeSpan(0, 0, 5);
        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR();
    }
}
public class MyHub : Hub
{
    public void Send(string name, string message)
    {
        Clients.All.addMessage(name, message);
    }
}
} 

it should look like this:

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

namespace SignalRSelfHost
{
class Program
{
    static void Main(string[] args)
    {
        // This will *ONLY* bind to localhost, if you want to bind to all addresses
        // use http://*:8080 to bind to all addresses. 
        // See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx 
        // for more information.
        string url = "http://localhost:8080";
        GlobalHost.Configuration.ConnectionTimeout  =   new TimeSpan(0, 0, 110);
        GlobalHost.Configuration.DisconnectTimeout  =   new TimeSpan(0, 0, 60);
        GlobalHost.Configuration.KeepAlive          =   new TimeSpan(0, 0, 5);
        using (WebApp.Start(url))
        {
            Console.WriteLine("Server running on {0}", url);
            Console.ReadLine();
        }
    }
}
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);
    }
}
}