之前发过一篇《说说KeyedService》,那时的KeyedService用法奇特,还一堆报错,觉得这个功能并不难,即使是在preview版上,也不应该这么低水准,毕竟是大厂出品吗。原来那篇文章如下:
说说KeyedService
桂素伟,公众号:桂迹说说KeyedService
在RC1版,果然修正了这些错误,不管是用Singleton,还是Scoped或Transient,都很完美的支持,使用起来还很丝滑,不管是在Service层还是在Map方法内,[FromKeyedServices("名称")]都很好的得到了支持。虽然像一些朋友说的那样,这个功能来晚了一些,但来了总比没有好。使用这个功能的小伙伴们,可以准备改代码升级到.NET8.0了,毕竟是个长期支持版。
using Microsoft.Extensions.DependencyInjection.Extensions;
var builder = WebApplication.CreateBuilder(args);
//builder.Services.AddKeyedSingleton<IConfigRepository, ISMSConfigRepository>("smsRep");
//builder.Services.AddKeyedSingleton<INotifyService, SMSService>("smsSev");
//builder.Services.AddKeyedSingleton<IConfigRepository, IEMailConfigRepository>("emailRep");
//builder.Services.AddKeyedSingleton<INotifyService, EMailService>("emailSev");
//builder.Services.AddKeyedScoped<IConfigRepository, ISMSConfigRepository>("smsRep");
//builder.Services.AddKeyedScoped<INotifyService, SMSService>("smsSev");
//builder.Services.AddKeyedScoped<IConfigRepository, IEMailConfigRepository>("emailRep");
//builder.Services.AddKeyedScoped<INotifyService, EMailService>("emailSev");
builder.Services.AddKeyedTransient<IConfigRepository, ISMSConfigRepository>("smsRep");
builder.Services.AddKeyedTransient<INotifyService, SMSService>("smsSev");
builder.Services.AddKeyedTransient<IConfigRepository, IEMailConfigRepository>("emailRep");
builder.Services.AddKeyedTransient<INotifyService, EMailService>("emailSev");
var app = builder.Build();
app.MapGet("/email", ([FromKeyedServices("emailSev")] INotifyService notifyService, string message) => new
{
Result = notifyService.Notify(message),
Messgae = message,
Type = notifyService.GetType().Name
});
app.MapGet("/sms", ([FromKeyedServices("smsSev")] INotifyService notifyService, string message) => new
{
Result = notifyService.Notify(message),
Messgae = message,
Type = notifyService.GetType().Name
});
app.Run();
public interface INotifyService
{
bool Notify(string message);
}
public class SMSService : INotifyService
{
private readonly Dictionary<string, dynamic> _configs;
private readonly ILogger<EMailService> _logger;
public SMSService([FromKeyedServices("smsRep")] IConfigRepository configRepository, ILogger<EMailService> logger)
{
_logger = logger;
_configs = configRepository.GetConfig();
}
public bool Notify(string message)
{
_logger.LogInformation($"{_configs["name"]},SMSService,这里根据配置文件完成短信的通知发送,Message:{message}");
return true;
}
}
public class EMailService : INotifyService
{
private readonly Dictionary<string, dynamic> _configs;
private readonly ILogger<EMailService> _logger;
public EMailService([FromKeyedServices("emailRep")] IConfigRepository configRepository, ILogger<EMailService> logger)
{
_logger = logger;
_configs = configRepository.GetConfig();
}
public bool Notify(string message)
{
_logger.LogInformation($"{_configs["name"]},EMailService,这里根据配置文件完成邮件的通知发送,Message:{message}");
return true;
}
}
public interface IConfigRepository
{
Dictionary<string, dynamic> GetConfig();
}
public class IEMailConfigRepository : IConfigRepository
{
public Dictionary<string, dynamic> GetConfig()
{
//从数据库中获取配置信息
return new Dictionary<string, dynamic>() { { "name", "email配置" } };
}
}
public class ISMSConfigRepository : IConfigRepository
{
public Dictionary<string, dynamic> GetConfig()
{
//从数据库中获取配置信息
return new Dictionary<string, dynamic>() { { "name", "sms配置" } }; ;
}
}