如何在Signal I hub的Unity IoC容器中注入依赖项?

问题描述 投票:2回答:1

我在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>
c# dependency-injection asp.net-mvc-5 signalr unity-container
1个回答
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)和容器,因为容器需要知道对象图以解决必要的依赖关系。

© www.soinside.com 2019 - 2024. All rights reserved.