Question

I have an asp.net process which also consumes messages from a servicebus (MassTransit). For webrequests my database session is resolved with a PerWebRequest lifestyle.

But when the process consumes a message from MassTransit I need the database session to have another lifestyle, as no HttpContext is available.

I have made this:

    public class PerRequestLifeStyleManager : ILifestyleManager
{
    readonly PerWebRequestLifestyleManager perWebRequestLifestyleManager;
    readonly PerThreadLifestyleManager perThreadLifestyleManager;

    public PerRequestLifeStyleManager()
    {
        perWebRequestLifestyleManager = new PerWebRequestLifestyleManager();
        perThreadLifestyleManager = new PerThreadLifestyleManager();            
    }

    public void Init(IComponentActivator componentActivator, IKernel kernel, ComponentModel model)
    {
        perWebRequestLifestyleManager.Init(componentActivator, kernel, model);
        perThreadLifestyleManager.Init(componentActivator, kernel, model);
    }

    public object Resolve(CreationContext context)
    {
        return GetManager().Resolve(context);
    }

    public bool Release(object instance)
    {
        return GetManager().Release(instance);
    }

    public void Dispose()
    {
        GetManager().Dispose();
    }

    ILifestyleManager GetManager()
    {
        if (HttpContext.Current != null)
        {
            return perWebRequestLifestyleManager;
        }

        return perThreadLifestyleManager;
    }
}

Can anyone tell me, if this is the right way to go? And if it isn't, what is?

Thanks.

EDIT: I have just updated the question with some code that seems to work (before it was untested). I still am eager to know if this - seen from a Windsor perspective - is safe and sound.

Was it helpful?

Solution

Try using one of the hybrid lifestyles.

OTHER TIPS

By using the Castle Windsor extension, you should just be able to have your ISession as a dependency on the constructor of the consumer class. That way, the container will manage the lifecycle of the ISession, and dispose of it once the consumer is disposed by MT.

If you need even more control, you can look at how the WindsorConsumerFactory is implemented to wrap the resolution and release of the consumer class instance around the delivery of the message to the consumer.

If you need to inject something beyond that, you can also use an interceptor:

Unit of work when using MassTransit

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