IOC(AutoFac)配置-学习资料

 IOC配置(先安装AutoFacMVC)
1、创建ioc容器,创建实例
var builder = new ContainerBuilder();
2、把当前程序集中的所有的Controller 都注册
builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();
3、获取所有相关类库的程序集
Assembly[] assemblies = new Assembly[] { Assembly.Load("RentHouse.Service") };

RentHouse.Service:对应的Service类库的名称

4、当请求这个程序集下的接口里面的方法时候。就会返回对应的Services类里面的实现
builder.RegisterAssemblyTypes(assemblies)
.Where(type => !type.IsAbstract
&& typeof(IServiceSupport).IsAssignableFrom(type))
.AsImplementedInterfaces().PropertiesAutowired();

IServiceSupport: 对应的接口层继承父类(接口层每个接口都继承父类,配置时直接控制父类即可达到对接口层所有接口的控制)
如果一个实现类中定义了其他类型的接口属性,会自动给属性进行“注入”
Assign:赋值
type1.IsAssignableFrom(type2) type2是否实现了type1接口/type2是否继承自type1
typeof(IServiceSupport).IsAssignableFrom(type)只注册实现了IServiceSupport的类
避免其他无关的类注册到AutoFac中

var container = builder.Build();
5、注册系统级别的DependencyResolver,这样当MVC框架创建Controller等对象的时候都是管Autofac要对象。
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

直接配置在Global中

 

复制代码
using Autofac;
using Autofac.Integration.Mvc;
using HPIT.RentHouse.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace HPIT.RentHouse.Admin
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            #region ioc配置
            //1、创建ioc容器,创建实例
            var builder = new ContainerBuilder();
            //2、把当前程序集中的所有的Controller 都注册
            builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();
            //3、获取所有相关类库的程序集
            Assembly[] assemblies = new Assembly[] { Assembly.Load("HPIT.RentHouse.Service") };
            //4、当请求这个程序集下的接口里面的方法时候。就会返回对应的Services类里面的实现
            builder.RegisterAssemblyTypes(assemblies)
            .Where(type => !type.IsAbstract
                    && typeof(IServiceSupport).IsAssignableFrom(type))
                    .AsImplementedInterfaces().PropertiesAutowired();
            //如果一个实现类中定义了其他类型的接口属性,会自动给属性进行“注入”
            //Assign:赋值
            //type1.IsAssignableFrom(type2)   type2是否实现了type1接口/type2是否继承自type1
            //typeof(IServiceSupport).IsAssignableFrom(type)只注册实现了IServiceSupport的类
            //避免其他无关的类注册到AutoFac中

            var container = builder.Build();
            //5、注册系统级别的DependencyResolver,这样当MVC框架创建Controller等对象的时候都是管Autofac要对象。
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            #endregion
        }
    }
}
View Code
复制代码

 

posted @   Aliezerofist  阅读(163)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统

阅读目录(Content)

此页目录为空

点击右上角即可分享
微信分享提示