在c#中使用两个具有不同生活方式的Simple Injector容器实例

时间:2018-02-13 17:17:54

标签: c# entity-framework simple-injector hangfire

我在Global.asax中有一个Web表单应用程序,我在其中建立Simple Injector容器,如下所示。我之所以这样做是因为我使用Hangfire来安排重复工作,并且它不会采用我目前用于应用程序的Scoped生活方式,因为它作为后台工作线程运行。当我为我的EF实体创建容器的两个实例时,我收到以下错误。

  

无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象

有人可以告诉我如何在我的网页表格中注册两个具有不同生活方式的容器。

     Sheets("AL-Monroe County Hospit-Fvar").Select
Sheets("AL-Monroe County Hospit-Fvar").Copy Before:=Sheets(3)
Sheets("AL-Monroe County Hospit-Fva (2").Select
Sheets("AL-Monroe County Hospit-Fva (2").Name = "Summary-Fvar2"
Range("A4").Select
ActiveCell.FormulaR1C1 = "Summary"
Range("G12").Select
ActiveCell.FormulaR1C1 = _
    "=SUM('AL-Monroe County Hospit-Fvar'!RC,'AL-Riverview Regional M-Fvar'!RC,'FL-Bethesda Hospital Ea-Fvar'!RC,'FL-Bethesda Hospital We W-Fvar'!RC,'FL-Jackson Health Syste-Fvar'!RC,'FL-Mount Sinai Medical -Fvar'!RC,'FL-Mount Sinai Medical-Fvar'!RC,'KY-Barbourville ARH Hos-Fvar'!RC,'KY-Hazard ARH Regional-Fvar'!RC,'KY-Highlands Regional M-Fvar'!RC,'KY-Mary Breckinridge A" & _
    "R-Fvar'!RC,'KY-McDowell ARH Hospita-Fvar'!RC,'KY-Middlesboro ARH Hosp-Fvar'!RC,'KY-Morgan County ARH Ho-Fvar'!RC,'KY-Tug Valley ARH Hospi-Fvar'!RC,'KY-Twin Lakes Regional-Fvar'!RC,'KY-Whitesburg ARH Hospi-Fvar'!RC,'MS-Baptist Medical Cent -Fvar'!RC,)" & _
    ""

Range("G12").Select
Application.CutCopyMode = False
Selection.Copy
Range("F12:H17,N12:O17,S12:T17,X12:Y17,F21:H22,N21:O22,S21:T22,X21:Y22,F28:H35,N28:O35,S28:T35,X28:Y35,F40:H42,N40:O42,S40:T42,X40:Y42,F50:H52,N50:O52,S50:T52,X50:Y52,F59:H59,N59:O59,S59:T59,X59:Y59,F67:H67,N67:O67,S67:T67,X67:Y67,F63:H65,N63:O65,S63:T65,X63:Y65").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False

Range("A2").Select
Application.CutCopyMode = False
Selection.ClearContents
Range("A1").Select

用于注册的Global.asax代码

ContainerConfig.BuildContainer();
var container = ContainerConfig.BuildContainerJobs();

public static Container BuildContainer()
{
    var container = new Container();
    container.Options.DefaultScopedLifestyle = new ExecutionContextScopeLifestyle();
    container.Register<TraceTimer>(Lifestyle.Scoped);
    container.Register<Entities>(() => new Entities(), Lifestyle.Scoped);
    container.Register<ReferenceDataCache>(
        () => ReferenceDataCacheFactory.Create(), Lifestyle.Scoped);

    var adapter = new SimpleInjectorAdapter(container);
    ServiceLocator.SetLocatorProvider(() => (IServiceLocator)adapter);
    ExecutionContextScopeManager.Current = (IExecutionContextScopeManager)adapter;
    return container;
}

public static Container BuildContainerJobs()
{
    var container = new Container();
    container.Options.DefaultScopedLifestyle = new ExecutionContextScopeLifestyle();

    container.Register<Entities>(() => new Entities(), Lifestyle.Transient);
    container.Register<ReferenceDataCache>(
        () => ReferenceDataCacheFactory.Create(), Lifestyle.Transient);

    var adapter = new SimpleInjectorAdapter(container);
    ServiceLocator.SetLocatorProvider(() => (IServiceLocator)adapter);
    ExecutionContextScopeManager.Current = (IExecutionContextScopeManager)adapter;
    return container;
}

1 个答案:

答案 0 :(得分:0)

Simple Injector不会抛出此异常,而是由Entity Framework抛出。此异常通常是由使用另一个 "from Name=John"内的一个DbContext创建的实体实例引起的。

不幸的是,我不能更具体,并指出你出错的地方以及如何解决这个问题,因为你的问题不包含适当的细节。