gpt4 book ai didi

c# - Asp.Net SignalR DependencyResolver 不解析集线器

转载 作者:行者123 更新时间:2023-11-30 17:43:59 26 4
gpt4 key购买 nike

我们有一个复杂的应用程序,其集线器分布在多个 dll 中。完整的应用程序使用 DI 和 LightCore。现在我们面临的问题是 SignalR 似乎没有使用我们编写的 DependencyResolver Adapter。

我们尝试了两种不同的方式来注入(inject)我们自定义的 IDependencyResolver:

GlobalHost.DependencyResolver = new LightCoreSignalRDependencyResolver();

var hc = new HubConfiguration(Resolver = new LightCoreSignalRDependencyResolver()); 
app.MapSignalR(hc);

在这两种情况下,我们的解析器都不会在创建集线器时被调用。更糟糕的是,我们得到的异常是集线器没有无参数构造函数。

我们还做了一个简短的测试,只是将自定义 IHubActivator 注入(inject)到 DefaultDependencyResolver 中。同样的结果。根本没有使用 HubActivator - 至少我们没有遇到断点和日志条目。

有人知道这里会出什么问题吗?

(在 MVC 和 WebApi 中,自定义依赖解析器按预期工作)

集线器使用构建器助手进行注册:

builder.Register<Hubs.LoggingHub, Hubs.LoggingHub>();

依赖解析器看起来像这样:

public class LightCoreSignalRDependencyResolver : DefaultDependencyResolver
{
private readonly IContainer _container;

public LightCoreSignalRDependencyResolver(IContainer container)
{
if (container == null) throw new ArgumentNullException("container");
_container = container;
}

public object GetService(Type serviceType)
{
try
{
return _container.Resolve(serviceType);
}
// IDependencyResolver implementations must not throw an exception
// but return null if type is not registered
catch (RegistrationNotFoundException registrationNotFoundException)
{
return base.GetService(serviceType);
}
}

public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return _container.ResolveAll(serviceType).Concat(base.GetServices(serviceType));
}
// IDependencyResolver implementations must not throw an exception
// but return an empty object collection if type is not registered
catch (RegistrationNotFoundException registrationNotFoundException)
{
return base.GetServices(serviceType);
}
}
}

集线器构造函数如下所示:

这个与默认解析器一起使用,并使用我们的内部 Bootstrap 来解析依赖项。 (丑陋但有效的方式 - 表明可以解决依赖关系)

    public RobotControlHub()
{
_Publisher = Booty.Container.Resolve<IPublisher>();
_MessageService = Booty.Container.Resolve<IRobotMessageRepectionService>();
_CommandsService = Booty.Container.Resolve<IRobotGenericCommandsService>();
_ConnectionsMapping = Booty.Container.Resolve<IRobotConnectionMappingRepository>();
}

这就是我们在 SignalR 中使用解析器时期望调用的构造函数。

    public RobotControlHub(
IRobotConnectionMappingRepository connectionsMapping,
IRobotGenericCommandsService commandsService,
IRobotMessageRepectionService messageService,
IPublisher publisher)
{
_Publisher = publisher;
_MessageService = messageService;
_CommandsService = commandsService;
_ConnectionsMapping = connectionsMapping;
}

但正如所说,它似乎甚至没有被调用。

最佳答案

有时候睡一觉忘记这个问题是件好事。第二天早上,您查看代码,发现缺少“覆盖”。正确代码:

public class LightCoreSignalRDependencyResolver : DefaultDependencyResolver
{
private readonly IContainer _container;

public LightCoreSignalRDependencyResolver(IContainer container)
{
if (container == null) throw new ArgumentNullException("container");
_container = container;
}

public **override** object GetService(Type serviceType)
{
try
{
return _container.Resolve(serviceType);
}
// IDependencyResolver implementations must not throw an exception
// but return null if type is not registered
catch (RegistrationNotFoundException registrationNotFoundException)
{
return base.GetService(serviceType);
}
}

public **override** IEnumerable<object> GetServices(Type serviceType)
{
try
{
return _container.ResolveAll(serviceType).Concat(base.GetServices(serviceType));
}
// IDependencyResolver implementations must not throw an exception
// but return an empty object collection if type is not registered
catch (RegistrationNotFoundException registrationNotFoundException)
{
return base.GetServices(serviceType);
}
}
}

关于c# - Asp.Net SignalR DependencyResolver 不解析集线器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30172849/

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