【问题标题】:Using Unit of Work with Windsor Castle inside SignalR Hub使用 SignalR Hub 内的 Windsor Castle 工作单元
【发布时间】:2013-01-04 22:39:26
【问题描述】:

我正在使用 SignalR 和 Windsor Castle 从事 ASPNET MVC 4 项目。 我的 mvc 控制器和 WebApi 控制器在 Castle 上正常工作和实例化,但我的 Unit of Work 类在 SignalR 的 Hub 内使用时没有实例化。

我有什么遗漏吗?

这是我的代码:

IocConfig 类

public static class IocConfig
    {
        public static IWindsorContainer Container { get; private set; }

        public static void RegisterIoc(HttpConfiguration config)
        {
            // Set the dependency resolver for Web API.
            var webApicontainer = new WindsorContainer().Install(new WebWindsorInstaller());
            GlobalConfiguration.Configuration.DependencyResolver = new WebApiWindsorDependencyResolver(webApicontainer);

            // Set the dependency resolver for Mvc Controllers
            Container = new WindsorContainer().Install(new ControllersInstaller());
            DependencyResolver.SetResolver(new MvcWindsorDependencyResolver(Container)); 
            ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(Container)); 
            var controllerFactory = new WindsorControllerFactory(Container.Kernel); 
            ControllerBuilder.Current.SetControllerFactory(controllerFactory); 
        }
    }
}

WebWindsorInstaller 类

 internal class WebWindsorInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(Component
                .For<RepositoryFactories>()
                .ImplementedBy<RepositoryFactories>()
                .LifestyleSingleton());

            container.Register(Component
                .For<IRepositoryProvider>()
                .ImplementedBy<RepositoryProvider>()
                .LifestylePerWebRequest());

            container.Register(Component
                .For<IGdpUow>()
                .ImplementedBy<GdpUow>()
                .LifestylePerWebRequest());

            container.Register(Classes
               .FromAssemblyContaining<Api.MessagesController>()
               .BasedOn<IHttpController>()
               .If(t => t.Name.EndsWith("Controller"))
               .LifestylePerWebRequest());

        }

ControllerInstaller 类

public class ControllersInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(FindControllers().Configure(c => c.LifestyleTransient()));

            container.Register(Component
                .For<RepositoryFactories>()
                .ImplementedBy<RepositoryFactories>()
                .LifestyleSingleton());

            container.Register(Component
                .For<IRepositoryProvider>()
                .ImplementedBy<RepositoryProvider>()
                .LifestylePerWebRequest());

            container.Register(Component
                .For<IGdpUow>()
                .ImplementedBy<GdpUow>()
                .LifestylePerWebRequest());
        }

        private static BasedOnDescriptor FindControllers()
        {
            return AllTypes.FromThisAssembly()
                .BasedOn<IController>()
                .If(t => t.Name.EndsWith("Controller"));
        }

集线器

using Microsoft.AspNet.SignalR.Hubs;

[HubName("iServerHub")]
public class IServerHub : Hub
{
    protected IGdpUow Uow;

    public void Send(string name, string message)
    {
        var messagesList = Uow.UdsMessages.GetAll();       
        Clients.All.broadcastMessage(messagesList);
    }
}

在 Send 方法中,Uow 为 null。

控制器继承自这个基类

 public abstract class CustomControllerBase : Controller
    {
        protected IGdpUow Uow { get; set; }
    }

当我在控制器中使用 Uow 时,它会被实例化。我不明白在集线器内使用它的区别。

注意

我正在按照此处找到的关于如何设置 Windsor Castle 以使其与 SignalR 一起使用的说明进行操作,但我无法对其进行测试,因为我收到以下错误(似乎无法创建代理):

Cannot read property client of undefined

表示未创建集线器的代理

这就是我在服务器中的方式

公共类 IServerHub:集线器

并且在客户端中像这样

var clientServerHub = $.connection.iServerHub;

我可以在这里看到动态创建的文件:

/GdpSoftware.Server.Web/signalr/hubs

那么,为什么没有创建代理?

这是我的安装文件

public class HubsInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Component
            .For<RepositoryFactories>()
            .ImplementedBy<RepositoryFactories>()
            .LifestyleSingleton());

        container.Register(Component
            .For<IRepositoryProvider>()
            .ImplementedBy<RepositoryProvider>()
            .LifestylePerWebRequest());

        container.Register(Component
            .For<IGdpUow>()
            .ImplementedBy<GdpUow>()
            .LifestylePerWebRequest());

        container.Register(Classes.FromThisAssembly()
            .BasedOn<Hub>()
            .LifestyleTransient());
    }
}

提前致谢!吉列尔莫。

【问题讨论】:

    标签: c# asp.net-mvc castle-windsor signalr windsor-3.0


    【解决方案1】:

    您希望它在哪里得到解决?我没有看到那个代码。

    我不太了解 ASP.NET MVC,但我上次使用它时,控制器通过构造函数接收依赖项。您的控制器的 ctor 看起来如何?他们是否将 IGdpUow 作为参数?如果不是如何注入?我认为 Windsor 只能在您拥有财产的公共二传手时才能注入 - Castle Windsor property injection

    所以我猜你的控制器有 ctors 而你的 IServerHub 没有(顺便说一句,在 .NET 中,这个名字对一个类有点误导)

    【讨论】:

    • 我选择您的答案作为正确答案,因为它为我指明了正确的解决方案。我按照这里的说明进行操作:blog.mirajavora.com/signalr-dependency-injection 然后我删除了使用 SignalR 的每个实例;并将其更改为使用 Microsoft.AspNet.SignalR;并在 HubInstaller 中添加了我在其他安装程序中的内容,以实例化 RepositoryFactories 和 IRepositoryProvider 等工作单元,使其工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多