-
-
Notifications
You must be signed in to change notification settings - Fork 263
Open
Labels
Description
I am trying to use keyed services (https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.servicecollectionserviceextensions.addkeyedsingleton?view=net-9.0-pp) but it appears that the current code supports it.
I'm using the following code
.ConfigureServices((_, serviceCollection) =>
{
serviceCollection.AddKeyedSingleton<DbConnection>("Database1", new SqlConnection(...));
serviceCollection.AddKeyedSingleton<DbConnection>("Database2", new SqlConnection(...));
})
and
internal void OnExecute
(
[FromKeyedServices("Database1")] DbConnection connection1,
[FromKeyedServices("Database2")] DbConnection connection2,
)
I think the solution is to update the BindParameters method in ReflectionHelper.cs
else
{
// Check for FromKeyedServicesAttribute
var keyedAttr = methodParam.GetCustomAttribute<FromKeyedServicesAttribute>();
if (keyedAttr != null)
{
if (command.AdditionalServices is not IKeyedServiceProvider keyedServiceProvider)
{
throw new InvalidOperationException("AdditionalServices does not support keyed service resolution.");
}
arguments[i] = keyedServiceProvider.GetKeyedService(methodParam.ParameterType, keyedAttr.Key)
?? throw new InvalidOperationException($"No keyed service found for type {methodParam.ParameterType} and key '{keyedAttr.Key}'.");
}
else
{
var service = command.AdditionalServices?.GetService(methodParam.ParameterType);
arguments[i] = service
?? throw new InvalidOperationException(Strings.UnsupportedParameterTypeOnMethod(method.Name, methodParam));
}
}