Question

I'm working in an ASPNET MVC 4 project using SignalR and Windsor Castle. I've mvc controller and WebApi controllers working and instantiating ok with Castle, but my Unit of Work class is not instantiated when it is used inside the Hub of SignalR.

Is there something I'm missing?

This is my code:

IocConfig class

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 class

 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 class

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

Hub

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

In the Send method Uow is null.

Controllers are inherited from this base class

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

And when I use Uow in the controller it is instantiated. I don't understand the difference with using it inside the hub.

NOTE

I'm following the instruction found here on how to set up Windsor Castle to make it work with SignalR, but I can't test it since I'm getting the following error (seems that the proxy cannot be created):

Cannot read property client of undefined

meaning that the proxy to the hub was not created

This is how I have it in the server

public class IServerHub : Hub

and like this in the client

var clientServerHub = $.connection.iServerHub;

I can see the dynamically created file here:

/GdpSoftware.Server.Web/signalr/hubs

So, Why the proxy is not created?

This is my installer file

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

Thanks in advance! Guillermo.

Was it helpful?

Solution

Where do you expect it to be resolved? I don't see that code.

I don't know ASP.NET MVC very good but last time I used it controllers were receiving dependencies through constructor. How your controllers' ctors look? Do they take IGdpUow as parameter? If not how it is injected? I think Windsor can only inject when you have public setter for property - Castle Windsor property injection.

So I guess your controllers have ctors and your IServerHub doesn't (BTW in .NET this name is a bit misleading for a class)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top