autofac 注入代码示例

using System.Reflection;
using System.Web.Compilation;
using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Configuration;
using ModelLibrary;

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;

private void Inject_Autofac()
        {
            //创建一个容器
            var builder = new ContainerBuilder();

            builder.RegisterAssemblyTypes();
            //注册所有的controller
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            //RegisterType方式:
            //builder.RegisterType<IService>().AsSelf().InstancePerDependency();
            //Register方式
            //builder.Register(c => new AService()).AsSelf().InstancePerDependency();


            // 自动注入的方式,不需要知道具体类的名称

            /* BuildManager.GetReferencedAssemblies()
             * 程序集的集合,包含 Web.config 文件的 assemblies 元素中指定的程序集、
             * 从 App_Code 目录中的自定义代码生成的程序集以及其他顶级文件夹中的程序集。
             */

            //获取包含继承了IService接口类的程序集
            var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>().Where(assembly => assembly.GetTypes().FirstOrDefault(type => type.GetInterfaces().Contains(typeof(IService))) != null);

            //注册程序集 registerAssemblyTypes
            var enumerable = assemblies as Assembly[] ?? assemblies.ToArray();
            if (enumerable.Any())
            {
                builder.RegisterAssemblyTypes(enumerable).Where(type => type.GetInterfaces().Contains(typeof(IService))).AsImplementedInterfaces().InstancePerDependency();
            }

            //把容器装入到微软默认的依赖注入容器中
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

        }


        private void _autoFac()
        {
            var builder = new ContainerBuilder();
            SetupResolveRules(builder);
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            //把容器装入到微软默认的依赖注入容器中
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }

        private void SetupResolveRules(ContainerBuilder builder)
        {
            //UI项目只用引用service和repository的接口,不用引用实现的dll。  
            //如需加载实现的程序集,将dll拷贝到bin目录下即可,不用引用dll  
            var IServices = Assembly.Load("ModelLibrary");


            //根据名称约定(服务层的接口和实现均以Service结尾),实现服务接口和服务实现的依赖  
            builder.RegisterAssemblyTypes(IServices)
              .Where(t => t.Name.EndsWith("Service"))
              .AsImplementedInterfaces();

        }

        private void conf_atuoFac()
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + "config/config.json";

            var config = new ConfigurationBuilder();
            config.AddJsonFile(path);

            var module = new ConfigurationModule(config.Build());

            var builder = new ContainerBuilder();
            builder.RegisterModule(module);

            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }

阅读更多
个人分类: AutoFac
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭