Rougamo.Extensions.DependencyInjection.GenericHost 4.0.0

Additional Details

New version strategy, see the following URL for details. https://github.com/inversionhourglass/Rougamo.DI/blob/master/README_en.md#versioning-guidelines.

There is a newer version of this package available.
See the version list below for details.
dotnet add package Rougamo.Extensions.DependencyInjection.GenericHost --version 4.0.0                
NuGet\Install-Package Rougamo.Extensions.DependencyInjection.GenericHost -Version 4.0.0                
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="Rougamo.Extensions.DependencyInjection.GenericHost" Version="4.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Rougamo.Extensions.DependencyInjection.GenericHost --version 4.0.0                
#r "nuget: Rougamo.Extensions.DependencyInjection.GenericHost, 4.0.0"                
#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.
// Install Rougamo.Extensions.DependencyInjection.GenericHost as a Cake Addin
#addin nuget:?package=Rougamo.Extensions.DependencyInjection.GenericHost&version=4.0.0

// Install Rougamo.Extensions.DependencyInjection.GenericHost as a Cake Tool
#tool nuget:?package=Rougamo.Extensions.DependencyInjection.GenericHost&version=4.0.0                

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>();
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Rougamo.Extensions.DependencyInjection.GenericHost:

Package Downloads
Rougamo.Extensions.DependencyInjection.AspNetCore

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

# Rougamo.DI

`Rougamo.DI`提供了一系列 Rougamo(https://github.com/inversionhourglass/Rougamo) 的IoC/DI扩展,使你在使用 [Rougamo](https://github.com/inversionhourglass/Rougamo) 时获得更好的IoC/DI交互体验。

## 当前扩展一览

|                                                  包名                                                  |                                          用途                                         |
|:------------------------------------------------------------------------------------------------------:|:-------------------------------------------------------------------------------------|
|  [Rougamo.Extensions.DependencyInjection.AspNetCore](#rougamoextensionsdependencyinjectionaspnetcore)  | 使用官方`DependencyInjection`,结合当前`HttpContext`,返回正确scope的`IServiceProvider` |
| [Rougamo.Extensions.DependencyInjection.GenericHost](#rougamoextensionsdependencyinjectiongenerichost) | 使用官方`DependencyInjection`,适用于非AspNetCore的Generic Host项目                     |

## Rougamo.Extensions.DependencyInjection.AspNetCore

### 快速开始

```csharp
// 注册Rougamo(注:如果你不使用IoC/DI功能,Rougamo默认是不需要注册操作的)
public static void Main(string[] args)
{
   var builder = WebApplication.CreateBuilder(args);
   // ...省略其他步骤
   builder.Services.AddRougamoAspNetCore();
   // ...省略其他步骤
}

// 在切面类型中获取IServiceProvider实例并使用
public class TestAttribute : MoAttribute
{
   public override void OnEntry(MethodContext context)
   {
       // 使用扩展方法GetServiceProvider获取IServiceProvider实例
       var services = context.GetServiceProvider();

       // 使用IServiceProvider
       var xxx = services.GetService<IXxx>();
   }
}
```

### 非HttpContext Scope

默认情况下通过`MethodContext`的扩展方法`MethodContext`只会尝试获取当前`HttpContext`对应scope的`IServiceProvider`,如果当前没有`HttpContext`,那么就会返回根`IServiceProvider`。这样的设计是因为一般而言我们不会在AspNetCore的项目中手动创建一个scope,如果你确实有手动创建scope的场景需求,请按如下的方式操作:

```csharp

public static void Main(string[] args)
{
   var builder = WebApplication.CreateBuilder(args);
   // ...省略其他步骤
   builder.Services.AddNestableHttpContextScopeAccessor();  // 额外注册步骤
   builder.Services.AddRougamoAspNetCore();
   // ...省略其他步骤
}

public class Cls(IServiceProvider services)
{
   public void M()
   {
       // 调用扩展方法CreateResolvableScope创建scope,这里如果使用CreateScope来创建,那么这个scope将无法在切面类型中获取到
       using var scope = services.CreateResolvableScope();
   }
}
```

## Rougamo.Extensions.DependencyInjection.GenericHost

```csharp
// 注册Rougamo(注:如果你不使用IoC/DI功能,Rougamo默认是不需要注册操作的)
public static void Main(string[] args)
{
   var builder = Host.CreateDefaultBuilder();
   // ...省略其他步骤
   builder.ConfigureServices(services => services.AddRougamoGenericHost());
   // ...省略其他步骤
}

// 在切面类型中获取IServiceProvider实例并使用
public class TestAttribute : MoAttribute
{
   public override void OnEntry(MethodContext context)
   {
       // 使用扩展方法GetServiceProvider获取IServiceProvider实例
       var services = context.GetServiceProvider();

       // 使用IServiceProvider
       var xxx = services.GetService<IXxx>();
   }
}
```