CordiSharp.Extensions.DependencyInjection 0.2.0

dotnet add package CordiSharp.Extensions.DependencyInjection --version 0.2.0
                    
NuGet\Install-Package CordiSharp.Extensions.DependencyInjection -Version 0.2.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="CordiSharp.Extensions.DependencyInjection" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CordiSharp.Extensions.DependencyInjection" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="CordiSharp.Extensions.DependencyInjection" />
                    
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 CordiSharp.Extensions.DependencyInjection --version 0.2.0
                    
#r "nuget: CordiSharp.Extensions.DependencyInjection, 0.2.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.
#:package CordiSharp.Extensions.DependencyInjection@0.2.0
                    
#: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=CordiSharp.Extensions.DependencyInjection&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=CordiSharp.Extensions.DependencyInjection&version=0.2.0
                    
Install as a Cake Tool

CordiSharp.Extensions.DependencyInjection

CordiSharp 与 Microsoft.Extensions.DependencyInjection(MSDI)的集成:注册根 Context 单例, 并挂载 MSDI 的 IServiceProvider,供插件在运行时解析 CLR 服务。

本包不含任何声明式插件清单。插件由宿主应用在运行时用 ctx.Plugin(...) 加载、用返回的 PluginHandle 卸载。插件由宿主自身驱动,无需单独的宿主包。日志桥接请见 CordiSharp.Extensions.Logging。

安装

dotnet add package CordiSharp.Extensions.DependencyInjection

快速上手

using CordiSharp;
using CordiSharp.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();

// 注册根 Context 单例(并挂载 MSDI provider)
services.AddCordiSharp();

// 宿主的 CLR 服务照常注册
services.AddSingleton<IGreeter, ConsoleGreeter>();

await using var provider = services.BuildServiceProvider();

// 插件在运行时加载
var ctx = provider.GetRequiredService<Context>();
var handle = ctx.Plugin(typeof(GreeterPlugin), new GreeterConfig { Message = "hi" });
await handle.Await();
// ...
await handle.DisposeAsync();   // 卸载

插件类自身的构造参数可直接声明宿主的 CLR 服务,无需手写工厂:

[Plugin("greeter")]
public sealed class GreeterPlugin : IPlugin<GreeterConfig>
{
    // Context 由框架注入 + provider 解析 IGreeter
    public GreeterPlugin(Context ctx, IGreeter greeter) { /* ... */ }

    public void Load(Context ctx, GreeterConfig config) { /* ... */ }
}

它注册了什么

成员 说明
根 Context(单例) TryAddSingleton:创建 Context.Create() 并挂载当前 IServiceProvider
ctx.ServiceProvider 由 UseServiceProvider 挂载,插件构造与 ctx.Resolve<T>() 时使用

为什么需要它:MSDI 与 CordiSharp 的双层 DI

MSDI 的容器在 BuildServiceProvider() 之后不可再变更,不能在运行时往里注册新服务。 因此运行时加载的插件不能通过向 MSDI 注册来提供服务。本包的定位就是维持这条边界:

  • MSDI 是宿主基础设施(静态、不可变):AddCordiSharp 只注册根 Context 单例并挂载 provider。插件在构造时用它解析宿主的 CLR 服务。
  • 插件之间/插件对宿主提供的服务走 CordiSharp 自己的注册表(动态、可卸载): ctx.Provide(...)、ctx.Get<T>(...)、ctx.Inject(...) 等,随插件 fiber 建立与销毁。

所以运行时加载插件时应把服务挂到 CordiSharp 的 Context 注册表,而不是往 ServiceCollection 里塞。

解析宿主服务

插件有两个途径取得 MSDI 服务:

  • 构造函数注入:插件类构造参数里写入宿主服务类型(或 Context ctx + IGreeter),由 PluginLoader 从挂载的 provider 解析。
  • ctx.Resolve<T>():在插件 body 中显式解析(C# 14 扩展成员):
extension(Context ctx)
{
    public T? Resolve<T>() where T : class
        => ctx.ServiceProvider?.GetService<T>();

    public object? Resolve(Type type)
        => ctx.ServiceProvider?.GetService(type);
}

用法:

var greeter = ctx.Resolve<IGreeter>();  // 或 ctx.Resolve(typeof(IGreeter))

注意:Resolve 是 C# 14 的 extension(Context ctx) 块扩展成员(非扩展方法), 需要用 C# 14 编译器(.NET 10 SDK)。若要在运行时加载、无生成器产物的插件中解析, 直接 ctx.Get("serviceName") 或 ctx.ServiceProvider?.GetService(...)。

API 参考

CordiSharpServiceCollectionExtensions

成员 说明
AddCordiSharp(this IServiceCollection) 注册 root Context 单例并挂载 provider。返回 IServiceCollection
UseServiceProvider(this Context, IServiceProvider) 把 MSDI provider 挂到已存在的根 Context(供插件构造用)

ContextServiceProviderExtensions

成员 说明
Resolve<T>() where T : class 从挂载的 IServiceProvider 解析(无则 null)
Resolve(Type) 同上,按 Type 解析

CordiSharpOptions 与 AddPlugin 已移除:插件不再通过声明式清单注册,而是由宿主在运行时用 ctx.Plugin(...) 加载。

环境要求

  • net10.0
  • 本包使用 C# 14 扩展成员(extension)语法,需要 .NET 10 SDK(C# 14 编译器)。
  • 依赖 Microsoft.Extensions.DependencyInjection.Abstractions。

相关包

包 职责
CordiSharp 运行时核心(Context、插件、服务、事件、effects),含源生成器与分析器
CordiSharp.Extensions.Logging CordiSharp 日志与 MEL 的双向桥接
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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
0.2.0 100 9/13/2026
0.1.0 124 8/29/2026