net程序温莎复合生活方式

net程序温莎复合生活方式

本文介绍了对于asp.net程序温莎复合生活方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net过程,也从一个servicebus(MassTransit)使用消息。对于webrequests我的数据库会话与PerWebRequest生活方式解决。

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.

但是,当这个过程从MassTransit消耗的消息我需要的数据库会话有另一个生活方式,因为没有HttpContext的是可用的。

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

我已经作出这样的:

    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?

感谢。

编辑:我刚刚更新了一些code,似乎工作问题(之前是未经测试)。我还是很渴望知道这一点 - 从温莎角度来看 - 是安全和无害的

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.

推荐答案

尝试使用的的。

这篇关于对于asp.net程序温莎复合生活方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:00