Lycoris.Autofac.Extensions 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Lycoris.Autofac.Extensions --version 1.0.2
                    
NuGet\Install-Package Lycoris.Autofac.Extensions -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Lycoris.Autofac.Extensions" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lycoris.Autofac.Extensions" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Lycoris.Autofac.Extensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Lycoris.Autofac.Extensions --version 1.0.2
                    
#r "nuget: Lycoris.Autofac.Extensions, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Lycoris.Autofac.Extensions@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Lycoris.Autofac.Extensions&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Lycoris.Autofac.Extensions&version=1.0.2
                    
Install as a Cake Tool

Autofac扩展,简单易用,做了简单扩展,目前不支持控制器的相关注册

一、替换系统自带的DI容器为Autofac

var builder = WebApplication.CreateBuilder(args);

// 替换系统自带的DI容器为Autofac
builder.Host.UseAutofacExtensions(builder =>
{
    // autofac模块注册
});

// Add services to the container.

builder.Services.AddControllers();

var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseAuthorization();

app.MapControllers();

app.Run();

二、服务注册

服务注册依旧使用特性的方式处理,在需要注册的服务类上引用特性[AutofacRegister(ServiceLifeTime.Scoped)]

AutofacRegisterAttribute特性属性详解

  • ServiceLifeTime:服务生命周期,同系统的一样有:Transient,Scoped,Singleton三种
  • IsGeneric:泛型注册,默认为:false,所注册的服务需要引入泛型的,比如仓储服务IRepository<Table,TPrimary>这类的需要将此属性设置为true
  • PropertiesAutowired:属性注入,Autofac特有的注入方式,默认为false
  • EnableClassInterceptors:开启AOP拦截支持,默认为false,只有开启了注册服务时候才会同时注入拦截器
  • Self:仅注册当前类不与接口绑定,适合模型类、Quartz的调度任务注册
  • InterfaceName:指定接口名称,当前类继承了多个接口时,可以通过设置此属性指定注册的接口名称
  • InterceptorType:添加指定AOP拦截器,适合当前类库仅个别服务使用到某些固定的一个拦截器使用,该属性设置后,会默认开启AOP拦截支持,同样的指定的AOP拦截器也需要实现 Castle.CoreIInterceptor 接口
  • InterceptorOrder:当前指定的AOP拦截器拦截优先级,数值越小优先级越高,不设置默认最高优先级
  • IsInterceptor:当前服务是否为AOP服务

以下举几个比较常用的注册举例

  • 1. 最常见的注册
// 瞬态服务注册
[AutofacRegister(ServiceLifeTime.Transient)]
public class BlogAppService : IBlogAppService

// 作用域服务注册
[AutofacRegister(ServiceLifeTime.Scoped)]
public class BlogAppService : IBlogAppService

// 单例服务注册
[AutofacRegister(ServiceLifeTime.Singleton)]
public class BlogAppService : IBlogAppService
  • 2. 泛型服务注册
[AutofacRegister(ServiceLifeTime.Scoped, IsGeneric = true)]
public class Repository<T, TPrimary> : IRepository<T, TPrimary> 
  • 3. 需要使用属性注入的服务注册
[AutofacRegister(ServiceLifeTime.Scoped, PropertiesAutowired = true)]
public class BlogAppService : ApplicationBaseService<BlogAppService>, IBlogAppService
  • 4. 需要开启AOP拦截的服务注册
[AutofacRegister(ServiceLifeTime.Scoped, PropertiesAutowired = true, EnableClassInterceptors = true)]
public class BlogAppService : ApplicationBaseService<BlogAppService>, IBlogAppService
  • 5. AOP拦截器注册
[AutofacRegister(ServiceLifeTime.Scoped, IsInterceptor = true)]
public class UnitOfWorkInterceptor : IInterceptor
  • 6. 当前服务指定额外添加的AOP拦截服务注册
[AutofacRegister(ServiceLifeTime.Scoped, InterceptorType = typeof(UnitOfWorkInterceptor))]
public class BlogAppService : IBlogAppService

三、模块注册

模块注册实现有两种方式

  • 1. 在需要使用到注册服务的类库创建一个类并继承扩展好的基类AutofacRegisterModule
 public class ApplicationModule : AutofacRegisterModule
 {
     protected override void InterceptorRegister()
     {
         // 当前类除了你需要使用的AOP注册外,不需要再做其他处理

         // 如果需要使用AOP拦截器,请在此处注入,Aop拦截器需要实现 Castle.Core 的 IInterceptor 接口
         // 添加AOP拦截器,如果有多个拦截器请注明拦截器拦截顺序,数值越小优先级越大,默认为0,数值必须大于0
         
         // 数据库事务
         AddInterceptor<UnitOfWorkInterceptor>(0);
         // 操作日志
         AddInterceptor<OperationLogInterceptor>(1);
     }
 }
  • 2. 在需要使用到注册服务的类库创建一个类并继承Autofac的基类Module
public class ApplicationAutofacModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        // do something
    }
}

在第一步替换系统DI容器的扩展中引入模块注册服务

var builder = WebApplication.CreateBuilder(args);

// 替换系统自带的DI容器为Autofac
builder.Host.UseAutofacExtensions(builder =>
{
    // 如果有多个类库的服务,每个类库都需要继承一次 Module 或者扩展封装好的 AutofacRegisterModule 并在此处依次添加即可
    builder.AddRegisterModule<ApplicationModule>();
});

// Add services to the container.

builder.Services.AddControllers();

var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseAuthorization();

app.MapControllers();

app.Run();

四、额外的扩展

除以上的基础使用外,又增加了额外的扩展,部分类库可能只有一两个服务,不想新建一个注册服务的类,那么可以使用以下方式注册

简单注册方式

// 注册瞬态服务
builder.Services.AddAutofacTransient<ApplicationAppService>();
builder.Services.AddAutofacTransient<IApplicationAppService, ApplicationAppService>();


// 注册作用域服务
builder.Services.AddAutofacScoped<ApplicationAppService>();
builder.Services.AddAutofacScoped<IApplicationAppService, ApplicationAppService>();

// 注册单例服务
builder.Services.AddAutofacSingleton<ApplicationAppService>();
builder.Services.AddAutofacSingleton<IApplicationAppService, ApplicationAppService>();

Try系列

// 注册瞬态服务
builder.Services.TryAddAutofacTransient<ApplicationAppService>();
builder.Services.TryAddAutofacTransient<IApplicationAppService, ApplicationAppService>();


// 注册作用域服务
builder.Services.TryAddAutofacScoped<ApplicationAppService>();
builder.Services.TryAddAutofacScoped<IApplicationAppService, ApplicationAppService>();

// 注册单例服务
builder.Services.TryAddAutofacSingleton<ApplicationAppService>();
builder.Services.TryAddAutofacSingleton<IApplicationAppService, ApplicationAppService>();

Try系列需要注意的是,Try只会甄别是用额外扩展注册服务,并不会甄别模块注册内的服务

其他注册设置

services.TryAddAutofacScoped<SalesUserAppService>(opt =>
{
    // 开启Autofac属性注入
    opt.PropertiesAutowired = true;
    // 开启AOP拦截
    opt.EnableClassInterceptors = true;
    // 设置AOP拦截器
    opt.AddInterceptor<UnitOfWorkInterceptor>(0);
});

同理,此处添加的拦截器依旧实现 Castle.CoreIInterceptor 接口,如果AddInterceptor则拦截器拦截顺序会按照添加顺序进行排序

其他注册设置以上所有扩展注册方式均有,此处就不一一展示了,自行体会吧。由于是私人制作的包,使用过程中,或许可能会有些许bug,请各位大大手下留情

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
8.2.0 93 5/19/2026
8.1.3 118 3/30/2026
8.1.2 239 10/9/2025
8.1.1 309 8/30/2025
8.1.0 197 6/7/2025
8.0.0 267 5/20/2024
8.0.0-rc 191 3/13/2024
6.0.8 428 3/27/2023
6.0.7 458 2/2/2023
6.0.6 455 1/30/2023
6.0.5 465 1/4/2023
6.0.4 461 12/6/2022
6.0.3 578 11/28/2022 6.0.3 is deprecated because it is no longer maintained.
6.0.1 492 11/19/2022
1.0.2 493 11/16/2022
1.0.1 519 11/14/2022