Slow SignalR HubConnection Start

时间:2016-09-10 15:36:36

标签: c# localhost client signalr self-hosting

我的问题:有没有办法加快初始连接到集线器?

详细信息:

我在自主式Webservice @Win 8.1上使用SignalR。 Hub应用程序获得了本地客户端和远程客户端。

我通过DNS,localhost和127.0.0.1通过以下方式获得SignalR访问权限:

'netsh http add urlacl url=http://<Replace>:<Port>/ user=Everyone'

通常所有客户端甚至本地客户端都在使用DNS。它有效。

我的问题是一个sporned子进程(使用c#Microsoft.AspNet.SignalR.Client.HubConnection)。它通常连接400ms。没关系。但有时需要几秒钟。

我尝试将此客户端切换到127.0.0.1和localhost,但没有任何更改。

然后初始连接,SignalR很快。

如果没有简单的方法,我必须切换回普通的UDP。

Hub的SignalR配置:

using System;
using System.Web.Http;
using Microsoft.Owin;
using Microsoft.Owin.Cors;
using Microsoft.Owin.FileSystems;
using Microsoft.Owin.StaticFiles;
using Owin;

[assembly: OwinStartup(typeof(SignalRStartUp))]
namespace Onsite
{
    public class SignalRStartUp
    {
        // Any connection or hub wire up and configuration should go here
        public void Configuration(IAppBuilder pApp)
        {
            try
            {
                pApp.UseCors(CorsOptions.AllowAll);
                pApp.MapSignalR();
                pApp.UseFileServer(true);

                var lOptions = new StaticFileOptions
                {
                    ContentTypeProvider = new CustomContentTypeProvider(),
                    FileSystem = new PhysicalFileSystem(Constants.Root)
                };

                pApp.UseStaticFiles(lOptions);

                // Configure Web API for self-host. 
                var lConfig = new HttpConfiguration();
                lConfig.Routes.MapHttpRoute("RemoteApi", "api/{controller}/{action}");
                lConfig.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional });
                pApp.UseWebApi(lConfig); 
            }
            catch (Exception lEx)
            {
                Logger.Error(lEx);
            }
        }
    }
}

在客户端启动:

public static string GetConnectionString(string pHost = null)
{
    var lHost = pHost ?? GetMainClientDns();
    return string.Format("http://{0}{1}", lHost, SignalRPort);
}

private void StartSignalR()
{
    try
    {
        var lConnectionString = GetConnectionString("127.0.0.1");

        var lStopWatch = new Stopwatch();
        lStopWatch.Restart();
        IsConnectingHost = true;

        _connection = new HubConnection(lConnectionString, string.Format("AccessKey={0}&Role={0}", Constants.AccessKeyPlugIn));
        _connection.Reconnected += SetConnected;
        _connection.Reconnecting += SetDisConnected;

        MTalkHub = _connection.CreateHubProxy("OnsiteHub");

        MTalkHub.On("RequestSetNext", RequestSetNext);
        MTalkHub.On("RequestSetPrevious", RequestSetPrevious);
        MTalkHub.On("RequestEcho", RequestEcho);

        _connection.TransportConnectTimeout = _transportConnectTimeout;

        var lTask = _connection.Start();
        lTask.Wait();

        lStopWatch.Stop();

        SetConnected();
     }
    catch (TargetInvocationException lEx)
    {
        IsDisconnected = true;
        Task.Run(() => TryToConnect());
        Logger.Fatal(string.Format("Server failed to start. Already running on: '{0}'", lConnectionString), lEx);
    }
    catch (Exception lEx)
    {
        IsDisconnected = true;
        Task.Run(() => TryToConnect());
        Logger.Fatal(string.Format("Connecting to: '{0}' failed!", lConnectionString.ToStringNs()));
    }
    finally
    {
        IsConnectingHost = false;
    }
}

1 个答案:

答案 0 :(得分:0)

在另一个问题的错误修正之后,我的长初始连接也消失了。

我发现:存在竞争条件,很少通过网络共享锁定FileAccess通过更新本地缓存缩略图来阻止Hub。

无论如何,谢谢!