无法通过Mediator使用作用域服务



我有一个运行.NET 5和C#9的ASP.NET Core应用程序。这也在后台运行一个Discord机器人。Startup.cs中的ConfigureServices()方法如下所示。

public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();

var client = new DiscordSocketClient(new DiscordSocketConfig
{
AlwaysDownloadUsers = true,
MessageCacheSize = 10000,

GatewayIntents = GatewayIntents.Guilds | GatewayIntents.GuildMessages |
GatewayIntents.GuildMessageReactions | GatewayIntents.GuildPresences,

LogLevel = LogSeverity.Info
});

var commandService = new CommandService(new CommandServiceConfig
{
LogLevel = LogSeverity.Debug,
DefaultRunMode = RunMode.Sync,
CaseSensitiveCommands = false,
IgnoreExtraArgs = false,
});

services
.AddMediatR(Assembly.GetEntryAssembly())
.AddHostedService<StartupService>()
.AddHostedService<DiscordListener>()
.AddScoped<ITestService, TestService>()
.AddSingleton(client)
.AddSingleton(provider =>
{
commandService.AddModulesAsync(Assembly.GetEntryAssembly(), provider);
return commandService;
})
.AddSingleton(Configuration);
}

正如您所看到的,我已经添加了ITestServiceTestService作为一个作用域服务。

public class TestService : ITestService
{
public async Task<string> GetString()
{
await Task.Delay(1);
return "hey";
}
}
public interface ITestService
{
Task<string> GetString();
}

然后,我将此服务注入到我的命令模块中。

public class TestModule : ModuleBase<SocketCommandContext>
{
private readonly ITestService _testService;
public TestModule(ITestService testService)
{
_testService = testService;
}
[Command("ping")]
public async Task Ping()
{
var str = await _testService.GetString();
await ReplyAsync(str);
}
}

但是,应用程序不响应ping命令。事实上,我的接收消息的处理程序根本没有被命中(我已经通过断点进行了检查(。这是侦听事件并发布相关MediatR通知的托管服务。

public partial class DiscordListener : IHostedService
{
private readonly DiscordSocketClient _client;
private readonly IServiceScopeFactory _serviceScopeFactory;
public DiscordListener(
DiscordSocketClient client,
IServiceScopeFactory serviceScopeFactory)
{
_client = client;
_serviceScopeFactory = serviceScopeFactory;
}

public Task StartAsync(CancellationToken cancellationToken)
{
_client.MessageReceived += MessageReceived;
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_client.MessageReceived -= MessageReceived;
return Task.CompletedTask;
}
// Creating our own scope here
private async Task MessageReceived(SocketMessage message)
{
using var scope = _serviceScopeFactory.CreateScope();
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
await mediator.Publish(new MessageReceivedNotification(message));
}
}

这就是处理通知的通知处理程序。

public class CommandListener : INotificationHandler<MessageReceivedNotification>
{
private readonly IConfiguration _configuration;
private readonly DiscordSocketClient _client;
private readonly CommandService _commandService;
private readonly IServiceProvider _serviceProvider;
public CommandListener(
IConfiguration configuration,
DiscordSocketClient client, 
CommandService commandService, 
IServiceProvider serviceProvider)
{
_configuration = configuration;
_client = client;
_commandService = commandService;
_serviceProvider = serviceProvider;
}

public async Task Handle(MessageReceivedNotification notification, CancellationToken cancellationToken)
{
if (!(notification.Message is SocketUserMessage message)
|| !(message.Author is IGuildUser user)
|| user.IsBot)
{
return;
}

var argPos = 0;
var prefix = _configuration["Prefix"];

if (message.HasStringPrefix(prefix, ref argPos))
{
var context = new SocketCommandContext(_client, message);
using var scope = _serviceProvider.CreateScope();
await _commandService.ExecuteAsync(context, argPos, scope.ServiceProvider);
}
}
}

澄清一下,_client.MessageReceoved += ...处的断点没有命中。如果我将ITestServiceTestService实现更改为Singleton,那么处理程序将被命中,命令将按预期工作。你知道我做错了什么吗?

如果你想查看完整的代码,这里是项目的GitHub回购。它不太大。

这是混合Singleton和scoped服务时的典型问题。如果您最终遇到一个单例正在解析作用域服务的情况,这是不允许的。

来自此处的文档https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.1

不要从单例解析作用域服务。在处理后续请求时,这可能会导致服务的状态不正确。可以:

从作用域或临时服务中解析单例服务。从另一个作用域或临时服务中解析作用域服务。默认情况下,在开发环境中,从另一个寿命较长的服务解析服务会引发异常。有关详细信息,请参阅范围验证。

还有更多关于https://dotnetcoretutorials.com/2018/03/20/cannot-consume-scoped-service-from-singleton-a-lesson-in-asp-net-core-di-scopes/amp/

相关内容

  • 没有找到相关文章

最新更新