SignalR-OWIN-多个端点

Dwa*_*ang 4 startup signalr owin

我有这样的情况。

派生模块的基类。
每个模块都有自己的信号中心。
我想将所有模块托管在单个主机中,并按模块名称分隔。
某些模块将共享集线器名称。

namespace domain.com.base
{
    public class BaseClass
    {
        public string ApplicationName;
    }
}

namespace domain.com.billing
{
    public class Billing : BaseClass
    {
        ApplicationName = "Billing";
    }
    public class NotificationHub : Hub
    {
        public void Credit(decimal amount)
        {
            Clients.All.Notify(amount);
        }
    }
}

namespace domain.com.reporting
{
    public class Reporting : BaseClass
    {
        ApplicationName = "Reporting";
    }
    public class ReportingHub : Hub
    {
        public Report GetReport(int Id)
        {
             return ReportModule.RetrieveReport(Id);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在OWIN.Startup.Configuration(IAppBuilder)中可以做这样的事情

namespace domain.com.external_access
{
    public void Configuration(IAppBuilder app)
    {
        var asmList = GlobalResolver.Applications();
        foreach(var asm in asmList)
        {
            app.MapSignalR(asm.ApplicationName,false);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有效地给你这样的东西...

http://domain.com.external_access/Reporting/hubs http://domain.com.external_access/Billing/hubs

pys*_*o68 5

实际上,这是可行的,即使它需要一些沉重的解决方法SignalR的紧密耦合GlobalHost


下面的答案基于我所记得的,我现在无法访问我的代码。如果有错误,我将尽快更新答案。

编辑:昨天晚上我没事。只需按照下面的操作


您需要实现自己的方法,IHubDescriptorProviderIHubActivator控制Hub对每个“端点”所找到的对象。此外,您需要为每个端点提供自己的实例HubConfiguration(继承自ConnectionConfiguration),而不使用全局主机依赖项解析器。这可能是这样的:

class CustomSignalRConnectionConfiguration : HubConfiguration
{
    public CustomSignalRConnectionConfiguration() 
    {
        this.Resolver = new DefaultDependencyResolver();

        // configure your DI here...
        var diContainer = new YourFavouriteDi();

        // replace IHubActivator
        this.Resolver.Register(
            typeof(IHubActivator), 
            () => diContainer.Resolve<IHubActivator>());

        // replace  IHubDescriptorProvider
        this.Resolver.Register(
            typeof(IHubDescriptorProvider), 
            () => diContainer.Resolve<IHubDescriptorProvider>());

    }
}
Run Code Online (Sandbox Code Playgroud)

对于单个端点,您可以执行以下操作:

app.Map("/endpointName", mappedApp => 
{   
    var config = new CustomSignalRConnectionConfiguration();
    mappedApp.MapSignalR(config);
});
Run Code Online (Sandbox Code Playgroud)

祝好运!