前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >.Net Core 学习之路-AutoFac的使用

.Net Core 学习之路-AutoFac的使用

作者头像
蓝夏
发布2018-05-02 10:15:33
1.5K0
发布2018-05-02 10:15:33
举报
文章被收录于专栏:bluesummerbluesummer

本文不介绍IoC和DI的概念,如果你对Ioc之前没有了解的话,建议先去搜索一下相关的资料

这篇文章将简单介绍一下AutoFac的基本使用以及在asp .net core中的应用

Autofac介绍

组件的三种注册方式

  1. 反射
  2. 现成的实例(new)
  3. lambda表达式 (一个执行实例化对象的匿名方法)

下面是一些简短的示例,我尽可能多的列出来一些常用的注册方式,同时在注释中解释下“组件”、“服务”等一些名词的含义

// 创建注册组件的builder var builder = new ContainerBuilder(); //根据类型注册组件 ConsoleLogger 暴漏服务:ILogger builder.RegisterType<ConsoleLogger>().As<ILogger>(); //根据类型注册组件 ConsoleLogger,暴漏其实现的所有服务(接口) builder.RegisterType<ConsoleLogger>().AsImplementedInterfaces(); // 根据实例注册组件 output 暴漏服务:TextWriter var output = new StringWriter(); builder.RegisterInstance(output).As<TextWriter>(); //表达式注册组件,这里我们是在构造函数时传参->"musection" 暴漏服务:IConfigReader builder.Register(c =new ConfigReader("mysection")).As<IConfigReader>(); //表达式注册组件,解析时传参 var service = scope.Resolve<IConfigReader>( new NamedParameter("section", "mysection")); //反射注册组件,直接注册了ConsoleLogger类(必须是具体的类),如果ConsoleLogger有多个构造函数,将会取参数最多的那个构造函数进行实例化 builder.RegisterType<ConsoleLogger>(); //反射注册组件,手动指定构造函数,这里指定了调用 MyComponent(ILogger log,IConfigReader config)的构造函数进行注册 builder.RegisterType<MyComponent>() .UsingConstructor(typeof(ILogger), typeof(IConfigReader)); //注册MySingleton类中的静态变量"Instance",ExternallyOwned()函数指定自己控制实例的生命周期,而不是由autofac自动释放 builder.RegisterInstance(MySingleton.Instance).ExternallyOwned(); //一个组件暴漏两个服务 builder.RegisterType<CallLogger>().As<ILogger>().As<ICallInterceptor>(); //注册当前程序集中以“Service”结尾的类 builder.RegisterAssemblyTypes(System.Reflection.Assembly.GetExecutingAssembly()).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces(); //注册"MyApp.Repository"程序集中所有的类 builder.RegisterAssemblyTypes(GetAssembly("MyApp.Repository")).AsImplementedInterfaces(); //构建一个容器完成注册 var rootcontainer = builder.Build(); //可以通过下面这种方式手动获取IConfigReader 的实现类 //这种手动解析的方式需要 从生命周期作用域内获取组件,以保证组件最终被释放 //不要直接从根容器rootcontainer中解析组件,很有可能会导致内存泄漏 using(var scope = rootcontainer.BeginLifetimeScope()) { var reader = scope.Resolve<IConfigReader>(); }

如果不止一个组件暴露了相同的服务, Autofac将使用最后注册的组件作为服务的提供方。 想要覆盖这种行为, 在注册代码后使用 PreserveExistingDefaults() 方法修改

生命周期

using(var scope = rootcontainer.BeginLifetimeScope()) 上面的这段代码创建了一个生命周期作用域 生命周期作用域是可释放的,在作用域内解析的组件一定要保证在using之内使用或者最后手动调用组件的Dispose()函数 避免引用类的生命周期大于被引用类的生命周期 :如service 引用 repository 如果service的生命周期为单例,repository的生命周期为perrequest。service不会释放,所以最终会造成相关的repository始终无法释放的情况(Captive Dependencies) 虽然我们需要尽可能的避免直接从根容器解析组件,但总有例外的情况,对于非单例的组件,一定不要忘记调用组件的Dispose函数,实际上对于非单例的组件,从项目架构上来说,理论上应该是从构造函数注入进去的而不是手动解析。 需要手动解析的应该为一些配置帮助类等

对于一个具体组件(类)的生命周期分为以下几种(后面的函数是autofac对应的函数):

  • 每个依赖一个实例(Instance Per Dependency) (默认) ----InstancePerDependency()
  • 单一实例(Single Instance) 单例 ----SingleInstance()
  • 每个生命周期作用域一个实例(Instance Per Lifetime Scope)----InstancePerLifetimeScope()
  • 每个匹配的生命周期作用域一个实例(Instance Per Matching Lifetime Scope)----InstancePerMatchingLifetimeScope()
  • 每个请求一个实例(Instance Per Request) asp.net web请求----InstancePerRequest()
  • 每次被拥有一个实例(Instance Per Owned) ----InstancePerOwned()

如果你以前在传统的ASP.NET MVC项目中用过autofac,需要注意一些区别:

  • .net Core中需要使用InstancePerLifetimeScope替代之前(传统asp.net)的InstancePerRequest,保证每次HTTP请求只有唯一的依赖实例被创建。InstancePerRequest请求级别已经不存在了
  • .net Core中Web Api与Mvc的注册方式一样
  • .net Core中不再需要注册控制器,控制器由.net core创建,不归autofac管理(除了控制器的构造函数),这也解释了为什么不再使用InstancePerRequest生命周期,但是可以通过AddControllersAsServices()函数改变,想要深入了解的可以查看:https://www.strathweb.com/2016/03/the-subtle-perils-of-controller-dependency-injection-in-asp-net-core-mvc/

AutoFac 在asp .net core中的使用

在.net core 中使用autofac还是比较简单的,相比于传统的asp.net web 项目,省去了很多步骤

引入nuget程序包:

  • Autofac
  • Autofac.Extensions.DependencyInjection

startup 中代码:

代码语言:javascript
复制
    public static IContainer AutofacContainer;
    // This method gets called by the runtime. Use this method to add services to the container.
    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        //注册服务进 IServiceCollection
        services.AddMvc();
        ContainerBuilder builder = new ContainerBuilder();
        //将services中的服务填充到Autofac中.
        builder.Populate(services);
        //新模块组件注册
        builder.RegisterModule<DefaultModuleRegister>();
        //创建容器.
        AutofacContainer = builder.Build();
        //使用容器创建 AutofacServiceProvider 
        return new AutofacServiceProvider(AutofacContainer);
    }

上面代码调用了builder的RegisterModule函数,这个函数需要传入一个TModule的泛型,称之为autofac的模块 模块的功能就是把所有相关的注册配置都放在一个类中,使代码更易于维护和配置,下面展示了DefaultModuleRegister中的代码

DefaultModuleRegister:

代码语言:javascript
复制
public class DefaultModuleRegister : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        //注册当前程序集中以“Ser”结尾的类,暴漏类实现的所有接口,生命周期为PerLifetimeScope
        builder.RegisterAssemblyTypes(System.Reflection.Assembly.GetExecutingAssembly()).Where(t => t.Name.EndsWith("Ser")).AsImplementedInterfaces().InstancePerLifetimeScope();
        builder.RegisterAssemblyTypes(System.Reflection.Assembly.GetExecutingAssembly()).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces().InstancePerLifetimeScope();
        //注册所有"MyApp.Repository"程序集中的类
        //builder.RegisterAssemblyTypes(GetAssembly("MyApp.Repository")).AsImplementedInterfaces();
    }

    public static Assembly GetAssembly(string assemblyName)
    {
        var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(AppContext.BaseDirectory + $"{assemblyName}.dll");
        return assembly;
    }
}

Configure函数中可以选择性的加上程序停止时Autofac的释放函数:

代码语言:javascript
复制
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
        //程序停止调用函数
        appLifetime.ApplicationStopped.Register(() => { AutofacContainer.Dispose(); });
    }

Controller中代码:

代码语言:javascript
复制
    private IUserSer _user;
    private IUserSer _user2;
    public HomeController(IUserSer user, IUserSer user2)
    {
        _user = user;
        _user2 = user2;
    }
    public IActionResult Index()
    {
        using (var scope = Startup.AutofacContainer.BeginLifetimeScope())
        {
            IConfiguration config = scope.Resolve<IConfiguration>();
            IHostingEnvironment env = scope.Resolve<IHostingEnvironment>();
        }
        string name = _user.GetName();
        string name2 = _user2.GetName();
        return View();
    }

可以看到,因为我们将IServiceCollection中的服务填充到了autofac中了,所以现在可以在任何位置通过AutoFac解析出来.net core默认注入的服务(IConfiguration,IHostingEnvironment等)了 正常项目使用中,我们应该将AutofacContainer放在一个公共的类库中以便各个工程均可调用

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-04-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Autofac介绍
    • 组件的三种注册方式
      • 生命周期
      • AutoFac 在asp .net core中的使用
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档