惠州模板网站建设,淘宝网站建设好评,成交型网站建设方案,wamp 网站开发首先做什么安装注意事项
使用AOP的时候需要安装Autofac.Extras.DynamicProxy,如果发现VS老是提示报错,需要把VS重启下才可以识别。
WebApi
注意事项:WebApi中多一个ApiController中构造注入功能。
注入和AOP拦截 var siteNameList ClassHelper.GetConstants(typeof(SiteName));//创建…安装注意事项
使用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().CastAssembly().ToList();//builder.RegisterAssemblyTypes(assemblys.ToArray())//查找程序集中以Service结尾的类型
//.Where(t t.Name.EndsWith(Service))
//.AsImplementedInterfaces();//拦截器注入builder.RegisterTypeAOPInterceptor();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.RegisterTypeService1().AsService1(); //这一句可以不要因为下面已经把当前程序集下的类注入了ico容器//注册拦截器到容器builder.RegisterTypeAOPInterceptor();//在注册类型到容器的时候动态注入拦截器//builder.RegisterTypeDeviceI().AsIDevice().EnableInterfaceInterceptors().InterceptedBy(typeof(AOPInterceptor));//注册当前程序集的所有类成员builder.RegisterAssemblyTypes(System.Reflection.Assembly.GetExecutingAssembly()).AsImplementedInterfaces().EnableInterfaceInterceptors().InterceptedBy(typeof(AOPInterceptor));_container builder.Build(); //只有在Build之后才能调用ResolveT()}public static T ResolveT(){return _container.ResolveT();}}