SignalR 自主机断开连接超时,没有全局.asax

本文关键字:全局 asax 超时 主机 断开 连接 SignalR | 更新日期: 2023-09-27 18:35:46

我正在使用带有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);
        }
    }
}

有什么建议可以解决这个问题吗?

文档链接:信号R 配置

SignalR 自主机断开连接超时,没有全局.asax

您需要在调用启动之前调用 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);
    }
}
}