1

我是 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));
        }
    }

但这行不通。我HttpContextStructureMapDependencyScope.cs.

4

1 回答 1

0

Your solution should contain class called DefaultRegistry, you can put registration in there. Nested container will automatically inherit this registration from the parent.

于 2016-09-07T11:33:39.310 回答