gpt4 book ai didi

c# - 没有 Global.asax 的 SignalR 自主机断开连接超时

转载 作者:行者123 更新时间:2023-11-30 22:01:26 27 4
gpt4 key购买 nike

我在 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

最佳答案

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

关于c# - 没有 Global.asax 的 SignalR 自主机断开连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27712784/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com