You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Injected service is of wrong type when registered multiple times with generic registration - Microsoft.Extensions.DependecyInjection #79938
When registring a service in the IServiceCollection of asp.net core >= 6.0 once with a concrete type and once as a generic registration you get the wrong types of instances from the ServiceProvider.
varbuilder=WebApplication.CreateBuilder(args);builder.Services.AddTransient<IProcessor<SampleEntity>,SampleEntityProcessor>();builder.Services.AddTransient(typeof(IProcessor<>),typeof(GenericProcessor<>));varapp=builder.Build();// two instances of type SampleEntityProcessor are produced;varprocessors=app.Services.GetServices<IProcessor<SampleEntity>>();app.Run();
Reproduction Steps
Create a new asp.net core 6 Project:
Program.cs
varbuilder=WebApplication.CreateBuilder(args);builder.Services.AddTransient<IProcessor<SampleEntity>,SampleEntityProcessor>();builder.Services.AddTransient(typeof(IProcessor<>),typeof(GenericProcessor<>));varapp=builder.Build();// two instances of type SampleEntityProcessor are produced;varprocessors=app.Services.GetServices<IProcessor<SampleEntity>>();app.Run();
It would be helpful to confirm my understanding of the precise desired behavior in cases like this, though. Trying to piece together the intent from the code:
Open generic registrations are always "lower precedence" than exact registrations.
The default IEnumerable registration is always "lower precedence" than open generic registrations.
Among otherwise equal registrations, earlier registrations are "lower precedence" than exact registrations.
When resolving the default IEnumerable registration, registrations are returned in order from lowest precedence to highest precedence.
@davidfowl that seems right; potentially the fix there did not fully resolve the problem.
I believe it's the same issue as this #68053. We should confirm though, it only happens when the environment is development right?
@schwarzr should confirm, but from my repro the code shown requires development environment because in that setup it is being triggered by ValidateOnBuild = true in the ServiceProviderOptions.
However, ValidateOnBuild is not the only way to trigger this. For example the following also repros for me:
void Main()
{
var sc = new ServiceCollection();
sc.AddTransient<IProcessor<string>, SampleProcessor>();
sc.AddTransient(typeof(IProcessor<>), typeof(GenericProcessor<>));
// not using build validation
using var s = sc.BuildServiceProvider(new ServiceProviderOptions { ValidateOnBuild = false });
s.GetService(typeof(IProcessor<string>)); // resolve this first: gives SampleProcessor
s.GetServices<IProcessor<string>>(); // now this gives [SampleProcessor, SampleProcessor]
}
public interface IProcessor<T> { }
public class SampleProcessor : IProcessor<string> { }
public class GenericProcessor<T> : IProcessor<T> { }
@schwarzr should confirm, but from my repro the code shown requires development environment because in that setup it is being triggered by ValidateOnBuild = true in the ServiceProviderOptions.
I have been able to reproduce this issue outside Visual Studio and without ValidateOnBuild = true. So no, i don't think it is limited do the development environment.
@davidfowl
One thing i recognized when i investigated this issue is, that if you register the generic version twice the resulting instances are: [SampleProcessor, GenericProcessor, SampleProcessor]
voidMain(){varsc=newServiceCollection();sc.AddTransient<IProcessor<string>,SampleProcessor>();sc.AddTransient(typeof(IProcessor<>),typeof(GenericProcessor<>));sc.AddTransient(typeof(IProcessor<>),typeof(GenericProcessor<>));// not using build validationusingvars=sc.BuildServiceProvider(newServiceProviderOptions{ValidateOnBuild=false});s.GetService(typeof(IProcessor<string>));// resolve this first: gives SampleProcessors.GetServices<IProcessor<string>>();// now this gives [SampleProcessor, GenericProcessor, SampleProcessor]}publicinterfaceIProcessor<T>{}publicclassSampleProcessor:IProcessor<string>{}publicclassGenericProcessor<T>:IProcessor<T>{}
and if you register the generic service first, it kind of works as expected.
voidMain(){varsc=newServiceCollection();sc.AddTransient(typeof(IProcessor<>),typeof(GenericProcessor<>));sc.AddTransient<IProcessor<string>,SampleProcessor>();// not using build validationusingvars=sc.BuildServiceProvider(newServiceProviderOptions{ValidateOnBuild=false});s.GetService(typeof(IProcessor<string>));// resolve this first: gives SampleProcessors.GetServices<IProcessor<string>>();// now this gives [GenericProcessor, SampleProcessor]}publicinterfaceIProcessor<T>{}publicclassSampleProcessor:IProcessor<string>{}publicclassGenericProcessor<T>:IProcessor<T>{}
Activity
schwarzr commentedon Dec 23, 2022
area-Extensions-DependencyInjection
madelson commentedon Jan 8, 2023
I'd be interested in working on this.
It would be helpful to confirm my understanding of the precise desired behavior in cases like this, though. Trying to piece together the intent from the code:
Is that all correct?
davidfowl commentedon Jan 8, 2023
I believe it's the same issue as this #68053. We should confirm though, it only happens when the environment is development right?
madelson commentedon Jan 8, 2023
@davidfowl that seems right; potentially the fix there did not fully resolve the problem.
@schwarzr should confirm, but from my repro the code shown requires development environment because in that setup it is being triggered by
ValidateOnBuild = truein theServiceProviderOptions.However,
ValidateOnBuildis not the only way to trigger this. For example the following also repros for me:davidfowl commentedon Jan 8, 2023
That raises the priority of this. This was assumed to only be broken in validation scenarios.
schwarzr commentedon Jan 8, 2023
@madelson
I have been able to reproduce this issue outside Visual Studio and without ValidateOnBuild = true. So no, i don't think it is limited do the development environment.
@davidfowl
One thing i recognized when i investigated this issue is, that if you register the generic version twice the resulting instances are: [SampleProcessor, GenericProcessor, SampleProcessor]
and if you register the generic service first, it kind of works as expected.
Assign correct slots for open generic service descriptors during IEnu…
steveharter commentedon Jun 8, 2023
Are there any requests to backport the fix to v7 and possible v6 (since it's LTS)?