Rougamo.Extensions.DependencyInjection.Microsoft 7.0.0-preview-1726686520

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

// Install Rougamo.Extensions.DependencyInjection.Microsoft as a Cake Tool
#tool nuget:?package=Rougamo.Extensions.DependencyInjection.Microsoft&version=7.0.0-preview-1726686520&prerelease                

Rougamo.DI

中文 | English

Rougamo.DI provides a set of IoC/DI extensions for Rougamo that enhance the IoC/DI interaction experience when using Rougamo.

Available Extensions

Package Name Description
Rougamo.Extensions.DependencyInjection.Microsoft Uses the official DependencyInjection and integrates with HttpContext to return the correct scoped IServiceProvider
Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore Uses Autofac and integrates with HttpContext to return the correct scoped ILifetimeScope
Rougamo.Extensions.DependencyInjection.Autofac Uses Autofac, suitable for non-AspNetCore projects
Rougamo.Extensions.DependencyInjection.Abstractions The base abstraction package for all other packages
Rougamo.Extensions.DependencyInjection.AspNetCore.Abstractions The base abstraction package for all AspNetCore-related packages

Versioning Guidelines

All version numbers follow the Semantic Versioning (SemVer) format

  1. The version numbers of the two foundational abstraction packages start from 1.0.0 and increase incrementally:
    • Rougamo.Extensions.DependencyInjection.Abstractions
    • Rougamo.Extensions.DependencyInjection.AspNetCore.Abstractions
  2. For Microsoft official DI extension packages, the major version matches the corresponding official package (e.g., Microsoft.Extensions.*):
    • Rougamo.Extensions.DependencyInjection.Microsoft
  3. For Autofac extension packages, the major version matches the corresponding official Autofac package:
    • Rougamo.Extensions.DependencyInjection.Autofac.AspNetCore
    • Rougamo.Extensions.DependencyInjection.Autofac

Rougamo.Extensions.DependencyInjection.Microsoft

Quick Start

Rougamo.Extensions.DependencyInjection.Microsoft depends on DependencyInjection.StaticAccessor. The initialization of the startup project is completed via DependencyInjection.StaticAccessor. For different types of project initialization, please refer to DependencyInjection.StaticAccessor.

For the startup project, reference DependencyInjection.StaticAccessor.Hosting:

dotnet add package DependencyInjection.StaticAccessor.Hosting

For non-startup projects, reference Rougamo.Extensions.DependencyInjection.Microsoft:

dotnet add package Rougamo.Extensions.DependencyInjection.Microsoft

// Register Rougamo (Note: If you're not using IoC/DI functionality, Rougamo does not require registration by default)
public static void Main(string[] args)
{
    // 1. Initialization. This example uses a generic host; for other project types, please refer to the readme of the DependencyInjection.StaticAccessor project.
    var builder = Host.CreateDefaultBuilder();

    builder.UsePinnedScopeServiceProvider(); // Initialization completed with this single step

    var host = builder.Build();

    host.Run();
}

// Retrieve and use an IServiceProvider instance in an aspect type
public class TestAttribute : MoAttribute
{
    public override void OnEntry(MethodContext context)
    {
        var xxx = context.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>();
    }
}

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>();
    }
}

Usage in Framework Projects

If your project is an older WebForm, WinForm, WPF, or similar project that does not use the Microsoft.Extensions.* packages, you can directly call the RegisterRougamo extension method when initializing the ContainerBuilder.

var builder = new ContainerBuilder();
builder.RegisterRougamo();
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.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 is compatible. 
.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

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 174 9/18/2024
8.0.0-preview-1726686900 156 9/18/2024
7.0.1 115 9/18/2024
7.0.0-preview-1726686520 109 9/18/2024
6.0.1 112 9/18/2024
6.0.0-preview-1726686074 96 9/18/2024
3.0.1 98 9/18/2024
3.0.0-preview-1726685652 104 9/18/2024

- 新增`Rougamo.Extensions.DependencyInjection.Microsoft`,删除`Rougamo.Extensions.DependencyInjection.GenericHost`和`Rougamo.Extensions.DependencyInjection.AspNetCore`
   
   Dependency Injection核心逻辑移动到`DependencyInjection.StaticAccessor`系列包中,`Rougamo.Extensions.DependencyInjection.Microsoft`仅提供肉夹馍`MethodContext`系列扩展方法

   启动项目引用`DependencyInjection.StaticAccessor.Hosting`
   > dotnet add package DependencyInjection.StaticAccessor.Hosting

   非启动项目引用`Rougamo.Extensions.DependencyInjection.Microsoft`
   > dotnet add package Rougamo.Extensions.DependencyInjection.Microsoft

   ```csharp
   // 注册Rougamo(注:如果你不使用IoC/DI功能,Rougamo默认是不需要注册操作的)
   public static void Main(string[] args)
   {
       // 1. 初始化。这里用通用主机进行演示,其他类型项目请查看DependencyInjection.StaticAccessor项目的readme
       var builder = Host.CreateDefaultBuilder();

       builder.UsePinnedScopeServiceProvider(); // 仅此一步完成初始化

       var host = builder.Build();

       host.Run();
   }

   // 在切面类型中获取IServiceProvider实例并使用
   public class TestAttribute : MoAttribute
   {
       public override void OnEntry(MethodContext context)
       {
           var xxx = context.GetService<IXxx>();
       }
   }
   ```

---