Autofac在Asp.net MVC中在 Areas拆分到不同DLL下的注入失败问题

Autofac在Asp.net MVC中在 Areas拆分到不同DLL下的注入失败问题

废话不多说,直接上代码

    public static class MvcContainer
    {
        public static IContainer Instance;

        public static System.Web.Mvc.IDependencyResolver Init(Func<ContainerBuilder, ContainerBuilder> func = null)
        {
            var builder = new ContainerBuilder();//新建容器用于注册组件
            MyBuild(builder);//注册组件
            func?.Invoke(builder);
            Instance = builder.Build();//利用构建器创建容器
            return new AutofacDependencyResolver(Instance);//返回针对MVC的解析器
        }

        public static void MyBuild(ContainerBuilder builder)
        {
            //注册仓储层
            Assembly repositoryAssembly = Assembly.Load("WenYuanAutoAutoFac.Repository");
            builder.RegisterAssemblyTypes(repositoryAssembly)
                .PublicOnly()   //只要public访问权限的
                .Where(cc => cc.IsClass)    //只要class类型的(排除值和interface类型)
                .AsImplementedInterfaces(); //自动以其实现的接口暴露(包括Dispose接口)

            //注册逻辑层
            Assembly serviceAssembly = Assembly.Load("Wangxc.AutoFac.Service");
            builder.RegisterAssemblyTypes(serviceAssembly)
                .PublicOnly()
                .Where(cc => cc.IsClass)
                .AsImplementedInterfaces();


            //注册MVC项目中拆分出来的不同Areas下面的Controller
            Assembly MvcAssembly = Assembly.Load("Portal");
            Assembly XsAssembly = Assembly.Load("XS");
            Assembly ReportAssembly = Assembly.Load("Report");
            Assembly KcAssembly = Assembly.Load("KC");
            Assembly[] Assemblys = new Assembly[] { MvcAssembly , XsAssembly,ReportAssembly,KcAssembly};
            builder.RegisterControllers(Assemblys);

        }
    }

 

posted @ 2020-12-28 09:16  不要摸我的腰  阅读(80)  评论(0编辑  收藏  举报