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>();
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值