gpt4 book ai didi

c# - 如何在 SignalR hub 的 Unity IoC 容器中注入(inject)依赖项?

转载 作者:太空宇宙 更新时间:2023-11-03 12:17:38 28 4
gpt4 key购买 nike

我有一个应用程序是在 ASP.NET MVC 5 框架的顶部用 c# 编写的。我实现了 Unity.Mvc进入我的项目。现在,我正在尝试将依赖项对象注入(inject)到我的 SignalR Hub 中。 .

我创建了一个名为 UnityHubActivator 的类

我的类(class)是这样的

public class UnityHubActivator : IHubActivator
{
private readonly IUnityContainer _container;

public UnityHubActivator(IUnityContainer container)
{
_container = container;
}

public IHub Create(HubDescriptor descriptor)
{
return (IHub)_container.Resolve(descriptor.HubType);
}
}

然后在我的 UnityConfig 类中,我将以下内容添加到我的 RegisterTypes 方法

var unityHubActivator = new UnityHubActivator(container);

container.RegisterInstance<IHubActivator>(unityHubActivator);

我的中心是这样的

[Authorize]
public class ChatHub : Hub
{
protected IUnitOfWork UnitOfWork { get; set; }

public ChatHub(IUnitOfWork unitOfWork)
: base()
{
UnitOfWork = unitOfWork;
}

}

但是当我运行集线器时,构造函数永远不会被调用,连接也永远不会发生。

如何正确使用 Unity 框架将依赖项注入(inject)我的中心?

已更新

我试过像这样创建一个自定义容器

public class UnitySignalRDependencyResolver: DefaultDependencyResolver
{
protected IUnityContainer Container;
private bool IsDisposed = false;

public UnitySignalRDependencyResolver(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}

Container = container.CreateChildContainer();
}

public override object GetService(Type serviceType)
{
if (Container.IsRegistered(serviceType))
{
return Container.Resolve(serviceType);
}

return base.GetService(serviceType);
}

public override IEnumerable<object> GetServices(Type serviceType)
{
if (Container.IsRegistered(serviceType))
{
return Container.ResolveAll(serviceType);
}

return base.GetServices(serviceType);
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

if(IsDisposed)
{
return;
}

if(disposing)
{
Container.Dispose();
}

IsDisposed = true;
}
}

然后这是我在 Startup 类中配置集线器的方式

public class Startup
{
public IUnityContainer Container { get; set; }
public Startup(IUnityContainer container)
{
Container = container;
}

public void Configuration(IAppBuilder app)
{
app.Map("/signalr", map =>
{
var resolver = new UnitySignalRDependencyResolver(Container);

var hubConfiguration = new HubConfiguration
{
Resolver = resolver
};

map.RunSignalR(hubConfiguration);
});
}
}

但现在仍在工作......从未调用过 hub 构造函数。

这是我如何从客户端调用我的集线器

<script src="~/Scripts/jquery.signalR-2.2.2.min.js"></script>
<script src="~/signalr/hubs"></script>
<script>

$(function () {
// Reference the auto-generated proxy for the hub.
var app = $.connection.chatHub;
console.log('Getting things ready....');

app.client.outOfTasks = function () {
console.log('Nothing to do here')
};

app.client.logError = function (message) {
console.log(message)
};

app.client.logNote = function (message) {
console.log(message)
};

app.client.assignTask = function (taskId) {
app.server.taskReceived();
console.log('task received!!!' + taskId);

};

// Start the connection.
$.connection.hub.start().done(function () {
console.log('Connection Started....');
});
});

</script>

最佳答案

对于该容器,UnitySignalRDependencyResolver 似乎是准确的。

摘自官方文档,

Dependency Injection in SignalR

尝试以下示例来配置启动

public class Startup{    
public void Configuration(IAppBuilder app) {
IUnityContainer container = GetContainer();

var resolver = new UnitySignalRDependencyResolver(container);

var config = new HubConfiguration {
Resolver = resolver
};
app.MapSignalR("/signalr", config);
}

IUnityContainer GetContainer() {
//...call the unity config related code.
var container = UnityConfig.Container;
//...any other code to populate container.

return container;
}
}

确保向容器注册必要的对象,包括集线器 (ChatHub),因为容器需要知道对象图才能解析必要的依赖关系。

关于c# - 如何在 SignalR hub 的 Unity IoC 容器中注入(inject)依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49019889/

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