我在ASP.NET MVC 5 Framework的顶部有一个用c#编写的应用程序。我在项目中实现了Unity.Mvc。现在,我正在尝试将依赖项对象注入我的SignalR Hub。
我创建了一个名为UnityHubActivator
我的班级看起来像这样
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
框架将依赖项注入我的集线器?
已更新
我试图像这样创建一个自定义容器
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);
});
}
}
但现在仍在工作......中心构造函数永远不会被调用。
以下是我如何从客户端调用我的集线器
<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>
答案 0 :(得分:1)
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
),因为容器需要知道对象图,以便解决必要的依赖关系。