Repository navigation
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
For an IEnumerable<> of services resolved using GetServices, we found that the list of services returned is different when using --environment=Development versus any other values including blank.
We have two service implementations defined. When in Production, service A and B are in the enumerable. In development, service A is in the list twice, without B.
Expected Behavior
Should always resolve service A and B regardless of the enviroment.
Steps To Reproduce
Given types:
public interface IMarker { }
public interface IBaseService<T> { }
public class BaseService : IBaseService<A> { }
public class GenericService<T> : IBaseService<T>
where T : IMarker
{
}
public class A : IMarker { }This test passes (I use Shouldly for assertions, sorry):
[Fact]
public void Should_resolve_correctly_directly()
{
var services = new ServiceCollection();
services.AddTransient<IBaseService<A>, BaseService>();
services.AddTransient(typeof(IBaseService<>), typeof(GenericService<>));
var serviceProvider = services.BuildServiceProvider();
var handlers = serviceProvider
.GetServices<IBaseService<A>>()
.ToList();
handlers.Count.ShouldBe(2);
var handlersTypes = handlers
.Select(h => h.GetType())
.ToList();
handlersTypes.ShouldContain(typeof(BaseService));
handlersTypes.ShouldContain(typeof(GenericService<A>));
}This test passes using the WebApplication.CreateBuilder method:
[Fact]
public void Should_resolve_correctly_with_no_env_set()
{
var builder = WebApplication.CreateBuilder();
builder.Services.AddTransient<IBaseService<A>, BaseService>();
builder.Services.AddTransient(typeof(IBaseService<>), typeof(GenericService<>));
var serviceProvider = builder.Services.BuildServiceProvider();
var handlers = serviceProvider
.GetServices<IBaseService<A>>()
.ToList();
handlers.Count.ShouldBe(2);
var handlersTypes = handlers
.Select(h => h.GetType())
.ToList();
handlersTypes.ShouldContain(typeof(BaseService));
handlersTypes.ShouldContain(typeof(GenericService<A>));
var app = builder.Build();
var appHandlers = app.Services
.GetServices<IBaseService<A>>()
.ToList();
appHandlers.Count.ShouldBe(2);
var appHandlersTypes = appHandlers
.Select(h => h.GetType())
.ToList();
appHandlersTypes.ShouldContain(typeof(BaseService));
appHandlersTypes.ShouldContain(typeof(GenericService<A>));
}This test fails:
[Fact]
public void Should_resolve_correctly_in_development()
{
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
EnvironmentName = Environments.Development
});
builder.Services.AddTransient<IBaseService<A>, BaseService>();
builder.Services.AddTransient(typeof(IBaseService<>), typeof(GenericService<>));
var serviceProvider = builder.Services.BuildServiceProvider();
var handlers = serviceProvider
.GetServices<IBaseService<A>>()
.ToList();
handlers.Count.ShouldBe(2);
var handlersTypes = handlers
.Select(h => h.GetType())
.ToList();
handlersTypes.ShouldContain(typeof(BaseService));
handlersTypes.ShouldContain(typeof(GenericService<A>));
var app = builder.Build();
var appHandlers = app.Services
.GetServices<IBaseService<A>>()
.ToList();
appHandlers.Count.ShouldBe(2);
var appHandlersTypes = appHandlers
.Select(h => h.GetType())
.ToList();
appHandlersTypes.ShouldContain(typeof(BaseService));
appHandlersTypes.ShouldContain(typeof(GenericService<A>));
}The first set of assertions succeeds when I build the service provider directly from builder.Services. Once I call builder.Build and use the app.Services to resolve, it gives me the incorrect set of results. Whether or not I build the service provider does not change the second assertion.
Exceptions (if any)
No response
.NET Version
6.0.101
Anything else?
No response
Activity
Tratcher commentedon Feb 10, 2022
Setting
EnvironmentName = Environments.DevelopmentcausesValidateOnBuild = trueto be set on the ServiceContainer. Here's a reduced repro without web components:jbogard commentedon Feb 10, 2022
I can't help but think this is partially my fault from #39540
davidfowl commentedon Feb 11, 2022
Likely another caching bug. We had one like this already.
3 remaining items
timmydo commentedon Aug 25, 2022
I was just bitten by this... We recently moved from a web host builder to host builder, and the fact that ValidateOnBuild was only set when HostingEnvironment.IsDevelopment() (CreateDefaultServiceProviderOptions) is tricky. In our case, the class had some string types in the ctor but it was never resolved by the DI container at runtime so it didn't matter that it couldn't resolve it. After the migration to the generic host provider, on dev boxes the service stopped starting up. But our CI pipeline/prod don't catch the error.
I'd prefer if ValidateOnBuild was always on or always off, not depending on the HostingEnvironment.
davidfowl commentedon Aug 25, 2022
Or always on
timmydo commentedon Aug 25, 2022
Always on is fine with me. We're always trying to minimize differences between environments.
vchirikov commentedon Oct 21, 2022
Another bug repro, ran into this recently:
@tarekgh / @davidfowl Is it possible to fix this at least in version 7.0?
tarekgh commentedon Oct 21, 2022
.NET 7.0 is done. This issue is planned for .NET 8.0 now.
vchirikov commentedon Jul 21, 2023
@buyaa-n @tarekgh , the release of .NET 8 is near, will it include the fix for this issue?
tarekgh commentedon Jul 21, 2023
@buyaa-n @steveharter can help advising about it. @vchirikov are you interested to submit a PR for it?
davidfowl commentedon Jul 21, 2023
Pretty sure this is fixed, can someone validate?
vchirikov commentedon Jul 23, 2023
@davidfowl, I've checked preview 4 bug was there, but looks like have been fixed in preview 6 (at least in my repro)
Proof:
Thanks :)
tarekgh commentedon Jul 23, 2023
Thanks @vchirikov for confirming. Closing the issue per your comment.