WCP.Service
8.0.0
dotnet add package WCP.Service --version 8.0.0
NuGet\Install-Package WCP.Service -Version 8.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="WCP.Service" Version="8.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WCP.Service" Version="8.0.0" />
<PackageReference Include="WCP.Service" />
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 WCP.Service --version 8.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: WCP.Service, 8.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.
#:package WCP.Service@8.0.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=WCP.Service&version=8.0.0
#tool nuget:?package=WCP.Service&version=8.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
WCP.Service
WCP.Service 提供了一套基于“标记接口 + 约定自动注册”的依赖注入辅助组件,用于在 ASP.NET Core 中简化服务层的注册工作。
通过定义服务接口继承不同生命周期的标记接口(ITransientDependency、IScopedDependency、ISingletonDependency),并在启动时调用一次 AddAutoService,即可自动将实现类批量注册到 DI 容器中。
- Target Framework:
net8.0 - License:
Apache-2.0 - 作者 / Author: 吴存平
安装 (Install)
在 NuGet 中搜索并安装:
- 包名:
WCP.Service
使用 .NET CLI:
dotnet add package WCP.Service
使用 Package Manager:
Install-Package WCP.Service
PackageReference 示例:
<ItemGroup>
<PackageReference Include="WCP.Service" Version="8.0.0" />
</ItemGroup>
核心概念 (Core Concepts)
命名空间:WCP.Service
标记接口:
IDependency:所有可被自动注入的服务接口的基础标记接口;ITransientDependency:标记为瞬时 (Transient) 生命周期的服务接口;IScopedDependency:标记为作用域 (Scoped) 生命周期的服务接口;ISingletonDependency:标记为单例 (Singleton) 生命周期的服务接口。
扩展方法:
ServiceExtensions.AddAutoService(this IServiceCollection services, params Type[] ignoreTypes)ServiceExtensions.AddAutoService(this IServiceCollection services, bool debug, params Type[] ignoreTypes)
AddAutoService会扫描当前 AppDomain 所有类型:
- 找出实现了
IDependency的接口和实现类;- 根据接口继承的是哪种生命周期标记接口,选择 AddTransient / AddScoped / AddSingleton 注册。
快速开始 (Quick Start)
1. 定义服务接口与实现类
using WCP.Service;
// 定义一个作用域生命周期的服务接口
public interface IUserService : IScopedDependency
{
string GetUserName(int id);
}
// 实现类无需实现标记接口,只需实现业务接口
public class UserService : IUserService
{
public string GetUserName(int id)
{
return $"User-{id}";
}
}
2. 在 Program.cs 中自动注册
using WCP.Service;
var builder = WebApplication.CreateBuilder(args);
// 自动扫描并注册所有 IDependency 派生接口
builder.Services.AddAutoService(debug: false /* 或 true 查看扫描日志 */);
var app = builder.Build();
app.Run();
此时,你可以在控制器或其他服务中直接通过构造函数注入 IUserService:
public class HomeController : Controller
{
private readonly IUserService _userService;
public HomeController(IUserService userService)
{
_userService = userService;
}
}
忽略特定接口 (Ignore Types)
如果你有某些公共接口不希望被自动注册,可以通过 ignoreTypes 参数排除:
builder.Services.AddAutoService(
debug: true,
ignoreTypes: new[] { typeof(IMyMarkerInterface) }
);
工作原理简述 (How It Works)
AddAutoService扫描当前AppDomain中所有类型;- 找出所有实现了
IDependency的接口和类; - 将接口与实现类两两匹配:
- 如果接口继承自
IScopedDependency,则AddScoped(interface, implement); - 如果继承自
ISingletonDependency,则AddSingleton(interface, implement); - 如果继承自
ITransientDependency,则AddTransient(interface, implement);
- 如果接口继承自
- 可选输出调试信息,帮助你查看自动注册的接口/实现类。
注意事项 (Notes)
- 建议将“服务接口”与“实现类”放在同一个程序集或同一个解决方案下的多个程序集,确保能被当前应用程序域扫描到;
- 对于生命周期敏感的服务(如使用 DbContext / HttpClient 等),请选择合适的标记接口;
AddAutoService会对所有实现IDependency的接口生效,请避免让“公共基础接口”直接继承IDependency,否则可能产生意外的自动注册行为。
License
本项目使用 Apache-2.0 许可证。详情请参阅 NuGet 包或源码中的 LICENSE 说明。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.