码迷,mamicode.com
首页 > Windows程序 > 详细

webform中使用webapi,并且使用autofac

时间:2015-11-19 07:06:45      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:

       private void AutofacIoCRegister()
        {
            HttpConfiguration config = GlobalConfiguration.Configuration;

            if (_containerProvider != null)
                _containerProvider = null;
            var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)); Type baseType = typeof(IDependency); var assemblies = System.Web.Compilation.BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList(); builder.RegisterAssemblyTypes(assemblies.ToArray()) .Where(type => baseType.IsAssignableFrom(type) && !type.IsAbstract) .AsSelf() //自身服务,用于没有接口的类 .AsImplementedInterfaces() //接口服务 .PropertiesAutowired() //属性注入 .InstancePerLifetimeScope(); //保证生命周期基于请求 IContainer container = builder.Build(); _containerProvider = new ContainerProvider(container); config.DependencyResolver = new AutofacWebApiDependencyResolver(container); //DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); }

webform中使用webapi,并且使用autofac

注意

IDependency 
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
 

webform中使用webapi,并且使用autofac

标签:

原文地址:http://www.cnblogs.com/shiningrise/p/4976470.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!