0

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

有什么我想念的吗?

这是我的代码:

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 为空。

控制器继承自这个基类

 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());
    }
}

提前致谢!吉列尔莫。

4

1 回答 1

1

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

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

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

于 2013-01-04T23:43:01.263 回答