1

我是IoC的新手,並且在我的MVC項目中安裝了Nuget Package StructureMap.MVC5
我想註冊以下:
For<HttpContextBase>().Use(() => new HttpContextWrapper(HttpContext.Current))我在哪裏爲StructureMap嵌套容器註冊HttpContextBase?

根據我正在讀我需要引用的嵌套集裝箱和我看到StructureMapDependencyScope.cs文件嵌套集裝箱的引用包裝中附帶。但是,我不知道在哪裏/如何註冊這個HttpContextBase。

我試圖從StructuremapMvc.cs文件中像這樣註冊它:

public static class StructuremapMvc { 

     public static StructureMapDependencyScope StructureMapDependencyScope { get; set; } 

     public static void End() { 
      StructureMapDependencyScope.Dispose(); 
     } 

     public static void Start() { 
      IContainer container = IoC.Initialize(); 
      StructureMapDependencyScope = new StructureMapDependencyScope(container); 

      StructureMapDependencyScope.CreateNestedContainer(); 
      StructureMapDependencyScope.CurrentNestedContainer.Configure(x => 
      { 
       x.For<IUserIdentity>().Use<CurrentUser>(); 
       x.For<HttpContextBase>().Use(() => new HttpContextWrapper(HttpContext.Current)); 
      }); 

      DependencyResolver.SetResolver(StructureMapDependencyScope); 
      DynamicModuleUtility.RegisterModule(typeof(StructureMapScopeModule)); 
     } 
    } 

但是,這不是工作。 StructureMapDependencyScope.cs內部的HttpContext屬性出現錯誤。

+0

你能用你收到的錯誤信息更新你的問題嗎?調用'StructuremapMvc.Start()'在哪裏? –

回答

0

您的解決方案應該包含名爲DefaultRegistry的類,您可以在其中註冊。嵌套容器將自動從父級繼承此註冊。