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
屬性出現錯誤。
你能用你收到的錯誤信息更新你的問題嗎?調用'StructuremapMvc.Start()'在哪裏? –