Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore
4.0.0-preview-1724259035
See the version list below for details.
dotnet add package Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore --version 4.0.0-preview-1724259035
NuGet\Install-Package Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore -Version 4.0.0-preview-1724259035
<PackageReference Include="Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore" Version="4.0.0-preview-1724259035" />
<PackageVersion Include="Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore" Version="4.0.0-preview-1724259035" />
<PackageReference Include="Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore" />
paket add Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore --version 4.0.0-preview-1724259035
#r "nuget: Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore, 4.0.0-preview-1724259035"
#:package Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore@4.0.0-preview-1724259035
#addin nuget:?package=Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore&version=4.0.0-preview-1724259035&prerelease
#tool nuget:?package=Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore&version=4.0.0-preview-1724259035&prerelease
Rougamo.DI
Rougamo.DI provides a set of IoC/DI extensions for Rougamo that enhance the IoC/DI interaction experience when using Rougamo.
Available Extensions
| Package | Purpose |
|---|---|
| Rougamo.Extensions.DependencyInjection.AspNetCore | Uses the official DependencyInjection with the current HttpContext to return the correct scoped IServiceProvider. |
| Rougamo.Extensions.DependencyInjection.GenericHost | Uses the official DependencyInjection, designed for non-AspNetCore Generic Host projects. |
Rougamo.Extensions.DependencyInjection.AspNetCore
Quick Start
// Register Rougamo (Note: Rougamo does not require registration if you do not need IoC/DI features)
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// ... other setup steps
builder.Services.AddRougamoAspNetCore();
// ... other setup steps
}
// Accessing IServiceProvider in an aspect
public class TestAttribute : MoAttribute
{
public override void OnEntry(MethodContext context)
{
// Use the extension method GetServiceProvider to obtain the IServiceProvider instance
var services = context.GetServiceProvider();
// Utilize IServiceProvider
var xxx = services.GetService<IXxx>();
}
}
Non-HttpContext Scope
By default, the extension method GetServiceProvider on MethodContext only attempts to retrieve the IServiceProvider for the current HttpContext scope. If there is no HttpContext, it will return the root IServiceProvider. This design assumes that in AspNetCore projects, scopes are typically not manually created. If you have a scenario where you need to manually create a scope, follow the steps below:
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// ... other setup steps
builder.Services.AddNestableHttpContextScopeAccessor(); // Additional registration step
builder.Services.AddRougamoAspNetCore();
// ... other setup steps
}
public class Cls(IServiceProvider services)
{
public void M()
{
// Use the extension method CreateResolvableScope to create a scope.
// If you use CreateScope, that scope will not be accessible in aspect types.
using var scope = services.CreateResolvableScope();
}
}
Rougamo.Extensions.DependencyInjection.GenericHost
// Register Rougamo (Note: Rougamo does not require registration if you do not need IoC/DI features)
public static void Main(string[] args)
{
var builder = Host.CreateDefaultBuilder();
// ... other setup steps
builder.ConfigureServices(services => services.AddRougamoGenericHost());
// ... other setup steps
}
// Accessing IServiceProvider in an aspect
public class TestAttribute : MoAttribute
{
public override void OnEntry(MethodContext context)
{
// Use the extension method GetServiceProvider to obtain the IServiceProvider instance
var services = context.GetServiceProvider();
// Utilize IServiceProvider
var xxx = services.GetService<IXxx>();
}
}
Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore
Quick Start
// Register Rougamo (Note: Rougamo does not require registration if you do not need IoC/DI features)
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Host
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
{
builder.RegisterRougamoAspNetCore();
});
// Registering IHttpContextAccessor is also required
builder.Services.AddHttpContextAccessor();
}
// Accessing ILifetimeScope in an aspect
public class TestAttribute : MoAttribute
{
public override void OnEntry(MethodContext context)
{
// Use the extension method GetAutofacCurrentScope to obtain the ILifetimeScope instance
var scope = context.GetAutofacCurrentScope();
// Utilize ILifetimeScope
var xxx = scope.Resolve<IXxx>();
}
}
Non-HttpContext Scope
By default, the GetAutofacCurrentScope extension method on MethodContext only attempts to retrieve the ILifetimeScope for the current HttpContext. If there is no HttpContext, it will return the root IServiceProvider. This design assumes that in AspNetCore projects, scopes are typically not manually created. If you have a scenario where you need to manually create a scope, follow the steps below:
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Host
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
{
builder.RegisterAutofacNestableHttpContextScopeAccessor(); // Additional registration step
builder.RegisterRougamoAspNetCore();
});
// Registering IHttpContextAccessor is also required
builder.Services.AddHttpContextAccessor();
}
public class Cls(IServiceProvider services)
{
public void M()
{
// Use the extension method BeginResolvableLifetimeScope to create a scope.
// If you use BeginLifetimeScope, that scope will not be accessible in aspect types.
using var scope = services.BeginResolvableLifetimeScope();
}
}
Rougamo.Extensions.DependencyInjection.Autofac
// Register Rougamo (Note: Rougamo does not require registration if you do not need IoC/DI features)
public static void Main(string[] args)
{
var builder = Host.CreateDefaultBuilder();
builder
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
{
builder.RegisterRougamo();
});
}
// Accessing ILifetimeScope in an aspect
public class TestAttribute : MoAttribute
{
public override void OnEntry(MethodContext context)
{
// Use the extension method GetAutofacCurrentScope to obtain the ILifetimeScope instance
var scope = context.GetAutofacCurrentScope();
// Utilize ILifetimeScope
var xxx = scope.Resolve<IXxx>();
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 is compatible. net5.0-windows was computed. 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 is compatible. 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 is compatible. 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. |
| .NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Autofac (>= 8.0.0)
- Autofac.Extensions.DependencyInjection (>= 8.0.0)
- Rougamo.Extensions.DependencyInjection.AspNetCore.Abstractions (>= 4.0.0-preview-1724259035)
- Rougamo.Extensions.DependencyInjection.Autofac (>= 4.0.0-preview-1724259035)
-
net5.0
- Autofac (>= 8.0.0)
- Autofac.Extensions.DependencyInjection (>= 8.0.0)
- Rougamo.Extensions.DependencyInjection.AspNetCore.Abstractions (>= 4.0.0-preview-1724259035)
- Rougamo.Extensions.DependencyInjection.Autofac (>= 4.0.0-preview-1724259035)
-
net6.0
- Autofac (>= 8.0.0)
- Autofac.Extensions.DependencyInjection (>= 8.0.0)
- Rougamo.Extensions.DependencyInjection.AspNetCore.Abstractions (>= 4.0.0-preview-1724259035)
- Rougamo.Extensions.DependencyInjection.Autofac (>= 4.0.0-preview-1724259035)
-
net7.0
- Autofac (>= 8.0.0)
- Autofac.Extensions.DependencyInjection (>= 8.0.0)
- Rougamo.Extensions.DependencyInjection.AspNetCore.Abstractions (>= 4.0.0-preview-1724259035)
- Rougamo.Extensions.DependencyInjection.Autofac (>= 4.0.0-preview-1724259035)
-
net8.0
- Autofac (>= 8.0.0)
- Autofac.Extensions.DependencyInjection (>= 8.0.0)
- Rougamo.Extensions.DependencyInjection.AspNetCore.Abstractions (>= 4.0.0-preview-1724259035)
- Rougamo.Extensions.DependencyInjection.Autofac (>= 4.0.0-preview-1724259035)
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.0.1 | 354 | 9/18/2024 |
| 8.0.1-preview-1726209340 | 211 | 9/13/2024 |
| 8.0.0 | 255 | 8/22/2024 |
| 7.0.1 | 234 | 9/18/2024 |
| 7.0.1-preview-1726208693 | 213 | 9/13/2024 |
| 7.0.0 | 281 | 8/22/2024 |
| 6.0.1 | 225 | 9/18/2024 |
| 6.0.1-preview-1726208008 | 206 | 9/13/2024 |
| 6.0.0 | 246 | 8/21/2024 |
| 5.0.1 | 232 | 9/18/2024 |
| 5.0.1-preview-1726207262 | 210 | 9/13/2024 |
| 5.0.0 | 243 | 8/21/2024 |
| 4.0.1 | 233 | 9/18/2024 |
| 4.0.1-preview-1726205254 | 229 | 9/13/2024 |
| 4.0.0 | 251 | 8/21/2024 |
| 4.0.0-priview-1724248067 | 267 | 8/21/2024 |
| 4.0.0-priview-1724239435 | 228 | 8/21/2024 |
| 4.0.0-preview-1724276951 | 220 | 8/21/2024 |
| 4.0.0-preview-1724276233 | 214 | 8/21/2024 |
| 4.0.0-preview-1724259035 | 236 | 8/21/2024 |
- 新增`Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore`,`Rougamo.Extensions.DependencyInjection.Autofac`
## Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore
### 快速开始
```csharp
// 注册Rougamo(注:如果你不使用IoC/DI功能,Rougamo默认是不需要注册操作的)
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Host
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
{
builder.RegisterRougamoAspNetCore();
});
// 注册IHttpContextAccessor也是必须的
builder.Services.AddHttpContextAccessor();
}
// 在切面类型中获取ILifetimeScope实例并使用
public class TestAttribute : MoAttribute
{
public override void OnEntry(MethodContext context)
{
// 使用扩展方法GetAutofacCurrentScope获取ILifetimeScope实例
var scope = context.GetAutofacCurrentScope();
// 使用ILifetimeScope
var xxx = scope.Resolve<IXxx>();
}
}
```
### 非HttpContext Scope
默认情况下通过`MethodContext`的扩展方法`GetAutofacCurrentScope`只会尝试获取当前`HttpContext`对应的`ILifetimeScope`,如果当前没有`HttpContext`,那么就会返回根`IServiceProvider`。这样的设计是因为一般而言我们不会在AspNetCore的项目中手动创建一个scope,如果你确实有手动创建scope的场景需求,请按如下的方式操作:
```csharp
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Host
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
{
builder.RegisterAutofacNestableHttpContextScopeAccessor(); // 额外的注册步骤
builder.RegisterRougamoAspNetCore();
});
// 注册IHttpContextAccessor也是必须的
builder.Services.AddHttpContextAccessor();
}
public class Cls(IServiceProvider services)
{
public void M()
{
// 调用扩展方法BeginResolvableLifetimeScope创建scope,这里如果使用BeginLifetimeScope来创建,那么这个scope将无法在切面类型中获取到
using var scope = services.BeginResolvableLifetimeScope();
}
}
```
## Rougamo.Extensions.DependencyInjection.Autofac
```csharp
// 注册Rougamo(注:如果你不使用IoC/DI功能,Rougamo默认是不需要注册操作的)
public static void Main(string[] args)
{
var builder = Host.CreateDefaultBuilder();
builder
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
{
builder.RegisterRougamo();
});
}
// 在切面类型中获取IServiceProvider实例并使用
public class TestAttribute : MoAttribute
{
public override void OnEntry(MethodContext context)
{
// 使用扩展方法GetAutofacCurrentScope获取ILifetimeScope实例
var scope = context.GetAutofacCurrentScope();
// 使用ILifetimeScope
var xxx = scope.Resolve<IXxx>();
}
}
```