第五章 Go微服务实战:服务间通信与gRPC错误处理

核心内容摘要

衡山派Luban-Lite MDI驱动调试指南:VIN_DEBUG宏详解与Log控制实战
2026快手涨粉全攻略:从0到万粉,技术人也能玩转的实操技巧

WAN2.2文生视频中文提示词工程:从草稿→结构化Prompt→高质量成片

目录什么是 Injectio安装基础用法1️⃣ 创建接口和实现类2️⃣ 在入口程序中调用 Injectio方法注册注册特性Attributes特性可选属性重复注册策略注册策略示例单例服务作用域服务瞬态服务工厂注册开放泛型Open Generic泛型特性需 .NET

0Keyed 服务需 Microsoft.Extensions.DependencyInjection

0添加到容器注册标签Tags

总结在 .NET Core/Web 项目中手动写一堆services.AddScoped...、AddSingleton...是否让你头大今天给大家介绍一个神器——Injectio帮你自动扫描并注册服务减少重复代码让你的依赖注入DI更加优雅。

什么是 InjectioInjectio是一个自动服务注册库通过约定Convention 特性Attribute自动扫描并注册依赖服务。

简单理解•不用手动注册服务•支持 Singleton / Scoped / Transient•支持工厂方法、模块注册、泛型和 Keyed 服务适用于.NET 6/7/8 Web 或控制台项目。

安装dotnet add package Injectio或者添加到.csproj文件中PackageReference IncludeInjectio PrivateAssetsall /PrivateAssetsall表示这个包不会被依赖它的项目引用。

基础用法1️⃣ 创建接口和实现类public interfaceIMessageService { string Send(string message); } [RegisterSingleton] // 自动注册为单例 publicclassEmailMessageService : IMessageService { public string Send(string message) { Console.WriteLine($Email sent: {message}); return message; } }2️⃣ 在入口程序中调用 Injectio对于 .NET 6/7/8 最小 APIusing Injectio; var builder WebApplication.CreateBuilder(args); // 自动注册项目中的服务自动生成的类可以看截图 builder.Services.AddInjectioDemo(); var app builder.Build(); app.MapGet(/, (IMessageService messageService) { messageService.Send(Hello from Injectio!); returnMessage sent!; }); app.Run();或者在控制器中使用[ApiController] [Route([controller]/[action])] publicclassWeatherForecastController : ControllerBase { privatereadonly IMessageService _myService; public WeatherForecastController(IMessageService myService) { _myService myService; } [HttpGet(Name GetWeatherForecast

] public string Get2() { return _myService.Send(Hello from WeatherForecastController!); } }这样就不用再写services.AddScopedIMessageService, EmailMessageService()了方法注册public class RegistrationModule { [RegisterServices] public static void Register(IServiceCollection services) { services .AddOptionsPollingOption() .ConfigureIConfiguration((settings, configuration) configuration.GetSection(PollingOption.SectionName).Bind(settings) ); } }image注册特性Attributes在类上添加注册特性源生成器会自动发现并注册服务。

特性说明[RegisterSingleton]标记为单例服务[RegisterScoped]标记为作用域服务[RegisterTransient]标记为瞬态服务[RegisterServices]标记方法用于注册服务特性可选属性•ServiceType指定注册接口•ImplementationType指定实现类型•Factory使用工厂方法创建实例•Duplicate重复注册策略Skip / Replace / Append•Registration注册策略Self / ImplementedInterfaces / SelfWithInterfaces重复注册策略•Skip已存在的服务跳过注册•Replace替换已有的服务注册•Append在已有服务后追加注册注册策略•Self将具体类型注册为自身•ImplementedInterfaces注册为其实现的接口•SelfWithInterfaces注册为自身及实现的接口示例单例服务[RegisterSingleton] public class SingletonService : IService { }指定服务类型[RegisterSingleton(ServiceType typeof(IService))] public class SingletonService : IService { }支持IEnumerableT多服务解析[RegisterSingleton(Duplicate DuplicateStrategy.Append)] public class SingletonService : IService { }作用域服务[RegisterScoped] public class ScopedService : IService { }瞬态服务[RegisterTransient] public class TransientService : IService { }工厂注册[RegisterTransient(Factory nameof(ServiceFactory))] publicclassFactoryService : IFactoryService { privatereadonly IService _service; public FactoryService(IService service) { _service service; } public static IFactoryService ServiceFactory(IServiceProvider serviceProvider) { returnnew FactoryService(serviceProvider.GetServiceIService()); } }开放泛型Open Generic[RegisterSingleton(ImplementationType typeof(OpenGeneric), ServiceType typeof(IOpenGeneric))] public class OpenGenericT : IOpenGenericT { }版本

0 支持自注册开放泛型[RegisterSingleton] public class OpenGenericT : IOpenGenericT { }泛型特性需 .NET

0[RegisterSingletonIService] public class ServiceImplementation : IService { }Keyed 服务需 Microsoft.Extensions.DependencyInjection

0[RegisterSingletonIServiceKeyed(ServiceKey Alpha)] public class ServiceAlphaKeyed : IServiceKeyed { } [RegisterSingletonIServiceKeyed(ServiceKey Beta)] public class ServiceBetaKeyed : IServiceKeyed { }使用枚举注册public enum ServiceType { Alpha, Beta } [RegisterSingletonIServiceKeyed(ServiceKey ServiceType.Alpha)] public class ServiceAlphaTypeKeyed : IServiceKeyed { } [RegisterSingletonIServiceKeyed(ServiceKey ServiceType.Beta)] public class ServiceBetaTypeKeyed : IServiceKeyed { }使用工厂方法注册[RegisterSingletonIServiceKeyed(ServiceKey Charlie, Factory nameof(ServiceFactory))] [RegisterSingletonIServiceKeyed(ServiceKey Delta, Factory nameof(ServiceFactory))] public class ServiceFactoryKeyed : IServiceKeyed { public ServiceFactoryKeyed(object? serviceKey) { ServiceKey serviceKey; } public object? ServiceKey { get; } public static IServiceKeyed ServiceFactory(IServiceProvider serviceProvider, object? serviceKey) { return new ServiceFactoryKeyed(serviceKey); } }添加到容器源生成器会生成一个扩展方法将所有发现的服务注册到容器中var services new ServiceCollection(); services.AddInjectioTestsConsole();自定义扩展方法名称通过 MSBuild 属性InjectioNamePropertyGroup InjectioNameLibrary/InjectioName /PropertyGroup ItemGroup CompilerVisibleProperty IncludeInjectioName / /ItemGroup

使用方法var services new ServiceCollection(); services.AddLibrary();注册标签Tags标记服务public interface IServiceTag { } [RegisterSingleton(Tags Client,FrontEnd)] public class ServiceTag : IServiceTag { }在注册方法中使用标签public static class ServiceRegistration { [RegisterServices] public static void Register(IServiceCollection services, ISetstring tags) { // 根据 tags 条件注册服务 } }添加到容器时指定标签不指定则注册所有服务var services new ServiceCollection(); services.AddInjectioTestsLibrary(Client);

总结Injectio让 .NET 项目的依赖注入变得自动化、优雅同时支持• 多种生命周期Singleton / Scoped / Transient• 工厂、模块、泛型、Keyed 服务• 标签控制和重复策略• 最小化手动注册减少 boilerplate如果你还在为一堆AddScoped、AddTransient头疼Injectio 会是你的救星引入地址

污污视频免费下载软件-污污视频免费下载软件应用

百度百家号客服电话人工服务

123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123