SignalR Self主机断开没有Global.asax的TimeOut

时间:2014-12-30 21:24:44

标签: c# asp.net signalr

我正在使用与OWIN自托管的SignalR 2.0。我正在尝试修改SignalR ConnectionTimeout属性,但它似乎不适用于OWIN Startup类。

当前尝试

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();
        }
    }
}

我还尝试添加自己从另一个项目复制的Global.asax文件。但是,我似乎无法解决System.Web.HttpApplication扩展名。

using System;
using Microsoft.AspNet.SignalR;

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

有任何建议如何解决这个问题?

链接到文档:SignalR Configuration

1 个答案:

答案 0 :(得分:3)

您需要在调用启动之前调用GlobalHost。基本上,如果您的代码如下所示:

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);
    }
}
} 

它应该是这样的:

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);
    }
}
}