我有一个简单的应用程序,它使用多个集线器,其中一些在同一个项目中,一个在另一个类库中,并且一切正常。现在我想尝试加载使用 CSharpCodeProvider.CompileAssemblyFromSource 在运行时创建的集线器,并让我的客户端与该集线器进行通信。它没有出现在/signalr/hubs/中。
作为我试图开始工作的几件事之一,我正在尝试使用自定义 IAssemblyLocator,如下所述:
https://github.com/SignalR/SignalR/wiki/Extensibility
但据我所知,我的 GetAssemblies() 代码并未被调用。这是我的 Global.asax.cs:
protected void Application_Start()
{
GlobalHost.DependencyResolver.Register(typeof(IAssemblyLocator), () => new AssemblyLocator());
var config = new HubConfiguration
{
EnableCrossDomain = true
};
RouteTable.Routes.MapHubs(config);
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
和自定义 IAssemblyLocator:
public class AssemblyLocator : IAssemblyLocator
{
public IList< Assembly > GetAssemblies()
{
throw new Exception("I will break stuff");
IList<Assembly> allAsms = BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList();
foreach( Assembly asm in HubFactory.HubAssemblies )
{
allAsms.Add( asm );
}
return allAsms;
}
}
正如你所看到的,我要做的第一件事就是抛出异常,只是为了证明这段代码正在被调用,但它并没有被抛出。使用此代码后,我的应用程序将继续正常工作,并且我的页面/客户端仍然可以向我的中心发送消息。
对于背景,我希望多个单独的数据源在同一网页上工作,使用一个连接,使用在运行时而不是编译时创建的集线器。所以我需要能够在运行时创建并加载集线器,让它出现在/signalr/hubs/中。我很确定我的运行时创建的集线器没问题 - 我已经获取了生成的代码,将其粘贴到常规 .cs 文件中并在编译时包含它,然后它在/signalr/hubs 中显示正常。由于共享连接,似乎无法使用具有一个集线器的组。我应该能够使用 GetHubContext(string) 找到运行时创建的集线器。
欢迎所有想法!
最佳答案
MapHubs 实际上取代了默认解析器。尝试更改顺序。
var config = new HubConfiguration
{
EnableCrossDomain = true
};
RouteTable.Routes.MapHubs(config);
GlobalHost.DependencyResolver.Register(typeof(IAssemblyLocator), () => new AssemblyLocator());
关于SignalR IAssemblyLocator 覆盖未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17063891/