Wcf Unity:在应用程序_BeginRequest中使用相同的datacontext?

Wcf Unity:在应用程序_BeginRequest中使用相同的datacontext?,wcf,linq-to-sql,dependency-injection,unity-container,datacontext,Wcf,Linq To Sql,Dependency Injection,Unity Container,Datacontext,以前,我成功地设置了unity,以便每次向存储库项目提供一个新的DataContext。它工作得很好 但例如,我使用的是一种WCF方法,它打开了两个服务,而这两个服务又打开了两个存储库(存储库模式)。。我希望能够在相同的wcf方法中重用相同的datacontext 因此,我一直在查看RegisterInstance,但每次都检查datacontext的GetHashCode及其不同之处。我想unity每次都会首先检查子容器,我假设我有一个实例设置-见下文 这是我的统一,执行一次 containe

以前,我成功地设置了unity,以便每次向存储库项目提供一个新的DataContext。它工作得很好

但例如,我使用的是一种WCF方法,它打开了两个服务,而这两个服务又打开了两个存储库(存储库模式)。。我希望能够在相同的wcf方法中重用相同的datacontext

因此,我一直在查看RegisterInstance,但每次都检查datacontext的GetHashCode及其不同之处。我想unity每次都会首先检查子容器,我假设我有一个实例设置-见下文

这是我的统一,执行一次

container.RegisterType<MyDataContext>(new TransientLifetimeManager(),
      new InjectionConstructor())
这是我的configureDataContextContainer

    public static void ConfigureDataContextContainer()
    {
        if (childContainer == null) // I have to do this otherwise it executes multiple times.
        {
            childContainer = Bootstrapper.Container.CreateChildContainer();
            childContainer.RegisterInstance<MyDataContext>
(container.Resolve<MyDataContext>());  // I Presume i create an instance here
        }
    }
public static void ConfigureDataContextContainer()
{
if(childContainer==null)//我必须这样做,否则它会执行多次。
{
childContainer=Bootstrapper.Container.CreateChildContainer();
childContainer.RegisterInstance
(container.Resolve());//我假设我在这里创建了一个实例
}
}
正如我在我的WCF方法中所说,我打开了两个服务,这两个服务依次打开了“它们自己的”响应,该响应接受DataContext—MyDataContext

为了解决BeginRequest的问题,我可以在我拥有的每个WCF方法上注册datacontext作为一个实例(如果它有效的话:-),但这似乎有点长

当然,每个连接(而不是会话)都有自己的DataContext是非常重要的

当我处理我的存储库时,我正在处理datacontext。。。现在(如果我能让它工作)我想我需要在EndRequest中处理这个。。否则,如果一个服务完成并处理了DataContext,而另一个服务没有找到,那么我将遇到问题

我希望我已经解释清楚了,:-)

概括地说,每个WCF方法都必须使用自己的datacontext,web方法可以调用多个服务(存储库模式),这些服务反过来将调用其存储库,该存储库需要unity注册的构造函数上的datacontext,但当前在同一个WCF方法中,多个服务调用这些存储库,它们获得自己的DataContext

如果我能澄清什么,请让我知道

谢谢

编辑

忘了提我是如何团结起来解决问题的。。。我简单地在容器(不是子容器)上调用它,然后服务调用respository

  using (IOfficeService officeService = Bootstrapper.Container.Resolve<IOfficeService >())
        {
使用(IOfficeService officeService=Bootstrapper.Container.Resolve())
{

您正在子容器中注册实例,因此在解析服务时必须使用子容器(此外,您应该在应用程序请求时处置子容器):

下面是我如何实现它的:

public class PerRequestLifetimeManager : LifetimeManager {
    private Guid key;

    public PerRequestLifetimeManager() {
        key = Guid.NewGuid();
    }

    public override object GetValue() {
        if (HttpContext.Current == null) {
            return null;
        } else {
            return HttpContext.Current.Items[key];
        }
    }

    public override void RemoveValue() {
        if (HttpContext.Current != null) {
            HttpContext.Current.Items.Remove(key);
        }
    }

    public override void SetValue(object newValue) {
        if (HttpContext.Current != null) {
            HttpContext.Current.Items.Add(key, newValue);
        }
    }
}

谢谢你,Michael,但是“PerRequestLifetime经理”是否将在整个过程中提供相同的DataContext?即在每个传入的请求上?因此,调用不同wcf方法的不同客户端都将获得相同的DataContext?这意味着在一个请求中将返回相同的实例。本质上,每个请求都有自己的单例。不同的客户端不会共享相同的ins太好了,一开始我很挠头,因为我找不到perRequestLifeTimeManager。但是我在网上找到了一个…它工作得很好。谢谢。我编辑了我的原始答案,将perRequestLifeTimeManager的实现包括在内,我忘了它没有包含在Unity中
using (var service = childContainer.Resolve<IOfficeService >())
{
}
Container.RegisterType<MyDataContext>(new PerRequestLifetimeManager());
public class PerRequestLifetimeManager : LifetimeManager {
    private Guid key;

    public PerRequestLifetimeManager() {
        key = Guid.NewGuid();
    }

    public override object GetValue() {
        if (HttpContext.Current == null) {
            return null;
        } else {
            return HttpContext.Current.Items[key];
        }
    }

    public override void RemoveValue() {
        if (HttpContext.Current != null) {
            HttpContext.Current.Items.Remove(key);
        }
    }

    public override void SetValue(object newValue) {
        if (HttpContext.Current != null) {
            HttpContext.Current.Items.Add(key, newValue);
        }
    }
}