我是 IoC 的新手,并将 Nuget 包安装StructureMap.MVC5
到我的 MVC 项目中。
我正在尝试注册以下内容:
For<HttpContextBase>().Use(() => new HttpContextWrapper(HttpContext.Current))
根据我正在阅读的内容,我需要在嵌套容器中引用它,并且我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));
}
}
但这行不通。我HttpContext
从StructureMapDependencyScope.cs
.