在 IIS 中使用 ARR 部署 signalR(应用程序请求路由)

Deploying signalR with ARR in IIS (Application Request Routing )

我们正在应用程序中使用 SignalR。异常类型为:

Hub Server was unable to start. Message:One or more errors occurred. Stack trace: at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken) at System.Threading.Tasks.Task.Wait() at ProjectName.TryStartHub(Object source, ElapsedEventArgs e)

虽然我们在我的系统中本地测试时没有错误。当我们用 ARR 部署它时。然后有一个例外,只是因为 ARR。虽然我们也删除了 ARR,然后尝试它工作。但它不适用于 ARR。

代码正确,但 SignalR 与 ARR 存在配置问题。

    public void InitializeHub()
    {
        appLog.Write("Initializing Hub Server");
        IHubProxy _hub;
        var querystringData = new Dictionary<string, string>();
        querystringData.Add("Key", "key1");
        hypervisorConnection = new HubConnection("url", querystringData);
        _hub = hConnection.CreateHubProxy("Hub");
        _hub.On<HypervisorCommand>("ExecuteHypervisorCommand", x => ExecuteHypervisorCommand(x));

        #region Initialize Hub Timer
        hHubTimer = new System.Timers.Timer();
        hHubTimer.Elapsed += new ElapsedEventHandler(TryStartHub);
        hHubTimer.AutoReset = false;
        hHubTimer.Interval = 1000;
        hHubTimer.Enabled = true;
        hHubTimer.Start();
        #endregion
    }

    private void TryStartHub(object source, ElapsedEventArgs e)
    {
        try
        {
            if (hConnection.State != ConnectionState.Connected)
            {
                hConnection.Start().Wait();
                appLog.Write("Hypervisor Hub server started.");
            }
        }
        catch (Exception ex)
        {
            appLog.Write("Hub Server was unable to start. Message:" + ex.Message + "\n Stack trace:" + ex.StackTrace);
        }
        hHubTimer.Interval = 30000;
        hHubTimer.Start();
    }

所以,我们终于解决了这个问题。问题出在 IIS 负载平衡器 ARR(应用程序请求路由)的配置中。

1.首先,Select IIS 菜单中的 ARR。 2。转到代理并将响应缓冲区阈值设置为 0。为什么我们必须将响应缓冲区大小设置为 0?这是详细说明: 默认情况下,这设置为 256kb,这意味着它将缓冲响应,直到它们达到该数量。通过将其设置为 0,ARR 将不再缓冲并且 SignlR 将正常运行。

3。然后转到负载均衡器并将负载均衡器算法从 "round robin" 更改为 "Server variable hash"。现在 SignalR 客户端每次都会连接到集线器。