Windsor composite lifestyle for asp.net process

浪子不回头ぞ 提交于 2019-12-12 19:25:17

问题


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.


回答1:


Try using one of the hybrid lifestyles.




回答2:


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



来源:https://stackoverflow.com/questions/7203745/windsor-composite-lifestyle-for-asp-net-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!