1 year ago

#289332

test-img

ZeniFlow

SignalR Castle Windsor - Is it possible to have different lifestyle for the same Castle Windsor container?

I'm using Castle Windsor as IOC in a Web Application, in this Web application I'm also using SignalR. To instanciate my signalR Hub i'm using a class that implements the IHubActivator SignalR interface as such :

public class SignalRHubActivator : IHubActivator
{
    private readonly IWindsorContainer _container;

    public SignalRHubActivator(IWindsorContainer container)
    {
        _container = container ?? throw new ArgumentNullException("container");
    }
    public IHub Create(HubDescriptor descriptor)
    {
        return _container.Resolve(descriptor.HubType) as IHub;
    }
}

But the problem is that it's using the same components registration configuration as the Web application as long as the container is the same. Knowing that my SignalR Hubs have dependencies to other services (also used for the Web App), but this services are registred as PerWebRequest (which makes sense in a Web application context), but with SignalR the PerWebRequest doesn't make sense because the HttpContext is not always set (when using WebSocket).

So my question is, is it possible to change the lifestyle of these dependencies only when it's used by a Hub. So when trying to resolve dependencies for a Hub instead of getting one from lifestyle PerWebRequest, is it possible to get one let's Transient for example.

I would like to have something like this :

In a Web context :

MyController (PerWebRequest): 
     - Service A (PerWebRequest)
         -Service B (PerWebRequest)
     - Service C  (PerWebRequest)

In a SignalR context :

MyHub (Transient) : 
     - Service A (Transient)
         -Service B (Transient)
     - Service C  (Transient)

with the same castle container. If not possible, would you advice me to have two separate containers ?

Thanks for reading me ! I hope i was clear. Feel free to ask if you don't understand :D

c#

signalr

castle-windsor

0 Answers

Your Answer

Accepted video resources