Don't create a service scope in CommandService#ExecuteAsync anymore

IServiceProvider does not support scopes by itself - this is a behavior
introduced by Microsoft's DI container. As such, not all DI containers
may support an IScopeFactory the way that Microsoft's DI is expecting
them to.

This also means that our builtin EmptyServiceProvider does not support
scopes - meaning that users who do not use a DI container can not invoke
commands.
This commit is contained in:
Christopher F
2018-03-13 18:23:42 -04:00
parent e68ef63bc6
commit f9ac190e9a

View File

@@ -283,8 +283,7 @@ namespace Discord.Commands
public async Task<IResult> ExecuteAsync(ICommandContext context, string input, IServiceProvider services = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
{
services = services ?? EmptyServiceProvider.Instance;
using (var scope = services.CreateScope())
{
var searchResult = Search(context, input);
if (!searchResult.IsSuccess)
return searchResult;
@@ -294,7 +293,7 @@ namespace Discord.Commands
foreach (var match in commands)
{
preconditionResults[match] = await match.Command.CheckPreconditionsAsync(context, scope.ServiceProvider).ConfigureAwait(false);
preconditionResults[match] = await match.Command.CheckPreconditionsAsync(context, services).ConfigureAwait(false);
}
var successfulPreconditions = preconditionResults
@@ -315,7 +314,7 @@ namespace Discord.Commands
var parseResultsDict = new Dictionary<CommandMatch, ParseResult>();
foreach (var pair in successfulPreconditions)
{
var parseResult = await pair.Key.ParseAsync(context, searchResult, pair.Value, scope.ServiceProvider).ConfigureAwait(false);
var parseResult = await pair.Key.ParseAsync(context, searchResult, pair.Value, services).ConfigureAwait(false);
if (parseResult.Error == CommandError.MultipleMatches)
{
@@ -369,8 +368,7 @@ namespace Discord.Commands
//If we get this far, at least one parse was successful. Execute the most likely overload.
var chosenOverload = successfulParses[0];
return await chosenOverload.Key.ExecuteAsync(context, chosenOverload.Value, scope.ServiceProvider).ConfigureAwait(false);
}
return await chosenOverload.Key.ExecuteAsync(context, chosenOverload.Value, services).ConfigureAwait(false);
}
}
}