当前位置: 首页 > news >正文

Autofac在WebApi,Winform中应用

安装注意事项

使用AOP的时候需要安装Autofac.Extras.DynamicProxy,如果发现VS老是提示报错,需要把VS重启下才可以识别。

WebApi

注意事项:WebApi中多一个ApiController中构造注入功能。

注入和AOP拦截

 var siteNameList = ClassHelper.GetConstants(typeof(SiteName));//创建容器var builder = new ContainerBuilder();//Api接口注入
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());//注册api容器的实现var assemblys = BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList();//builder.RegisterAssemblyTypes(assemblys.ToArray())//查找程序集中以Service结尾的类型
//.Where(t => t.Name.EndsWith("Service"))
//.AsImplementedInterfaces();//拦截器注入builder.RegisterType<AOPInterceptor>();Type baseType = typeof(IDependency);//注册DLL并开启接口拦截,通过拦截器
// 获取所有相关类库的程序集
// 先注册匹配非工厂名开头的Service
builder.RegisterAssemblyTypes(assemblys.ToArray()).Where(type => baseType.IsAssignableFrom(type) && !type.GetTypeInfo().IsAbstract&& type.Name.EndsWith("Service")&& !siteNameList.Contains(type.Name.Substring(0, 4))).AsImplementedInterfaces().InstancePerLifetimeScope().EnableInterfaceInterceptors().InterceptedBy(typeof(AOPInterceptor));//InstancePerLifetimeScope 保证对象生命周期基于请求//后注册匹配工厂名开头的Service
builder.RegisterAssemblyTypes(assemblys.ToArray()).Where(type => baseType.IsAssignableFrom(type) && !type.GetTypeInfo().IsAbstract&& type.Name.EndsWith("Service")&& siteNameList.Contains(type.Name.Substring(0, 4)) && type.Name.Substring(0, 4) == AbstractFactory.siteConfig.SiteName).AsImplementedInterfaces().InstancePerLifetimeScope().EnableInterfaceInterceptors().InterceptedBy(typeof(AOPInterceptor));//InstancePerLifetimeScope 保证对象生命周期基于请求Autofac.IContainer container = builder.Build();var configuration = GlobalConfiguration.Configuration;//WebApi整个的解析依赖交给AutoFac     //默认构造函数注入
configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);//注册api容器需要使用HttpConfiguration对象//把所有类型都注册到AutoFac容器里,最后把整个项目的实例创建和解析依赖交给AutoFac,//这样AutoFac在创建控制器实例的时候根据参数类型(不注入的走无参构造函数),去容器取对应实例进行注入

ApiController中构造注入功能

public class PATController : ApiController
{private readonly IPATService patService;public PATController(IPATService _patService){patService = _patService;}
}

Winform

注意事项:直接以接口的方式进行注入。
注入和AOP拦截

  public class AutofacBuilder{private static IContainer _container;public static void Init(){ContainerBuilder builder = new ContainerBuilder();// builder.RegisterType<Service1>().As<Service1>(); //这一句可以不要,因为下面已经把当前程序集下的类注入了ico容器//注册拦截器到容器builder.RegisterType<AOPInterceptor>();//在注册类型到容器的时候动态注入拦截器//builder.RegisterType<DeviceI>().As<IDevice>().EnableInterfaceInterceptors().InterceptedBy(typeof(AOPInterceptor));//注册当前程序集的所有类成员builder.RegisterAssemblyTypes(System.Reflection.Assembly.GetExecutingAssembly()).AsImplementedInterfaces().EnableInterfaceInterceptors().InterceptedBy(typeof(AOPInterceptor));_container = builder.Build();  //只有在Build之后,才能调用Resolve<T>()}public static T Resolve<T>(){return _container.Resolve<T>();}}
http://www.lryc.cn/news/145230.html

相关文章:

  • uview ui 查看版号
  • Python 爬虫网页图片下载到本地
  • PyQt open3d 加载 显示点云
  • Linux搭建SSLVpn
  • Qt5升级到Qt6分步迁移教程
  • 多线程学习之线程池
  • Elasticsearch基础
  • 论文阅读:Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks
  • 基于Web的旅游推荐网站设计与实现(论文+源码)_kaic
  • 继承AndroidView Model的错误
  • 智慧互联,有序充电--多场景充电
  • yum install libreoffice‘ returned a non-zero
  • Linux知识点 -- 网络基础(一)
  • 【leetcode刷题之路】剑指Offer(4)——分治+排序算法+动态规划
  • 美创科技“签”手柠檬文才学堂,共推高校数据安全建设
  • 【JAVA基础】数据类型,逻辑控制
  • 计算机竞赛 基于图像识别的跌倒检测算法
  • 计算机竞赛 基于大数据的股票量化分析与股价预测系统
  • input子系统
  • mac 10.13.6安装后开发准备工作
  • C++ using关键字
  • 让你对es有一个初步的了解
  • 编绎和优化,脚本代码小米加步枪赶超英法美
  • 数字电路-二进制学习
  • 运维Shell脚本小试牛刀(一)
  • screen命令,可以断开服务器连接,依旧能运行你的程序了
  • 【ArcGIS Pro二次开发】(63):批量更改字段别名
  • redis全套参数配置及降级解决方案
  • AMD即将上市大量中端显卡,为新显卡支付过高价格的日子可能结束
  • go学习一之go的初体验