Open skuzminoff opened 2 years ago
I tried your code, but it actually works on my machine. Could it be that you didn't call services.AddSimpleInjector
from inside your Startup.ConfigureServices
method? This method adds the Container
to the IServiceCollection
.
I tried to follow documentation https://docs.simpleinjector.org/en/latest/aspnetintegration.html
But anyway here is configuration code from the Startup.cs:
public class Startup
{
//....
private readonly Container _container = new Container();
//...
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers(...);
//...
services.AddSimpleInjector(_container, opts =>
{
opts.AddAspNetCore()
.AddControllerActivation();
opts.AddLogging();
});
SimpleInjectorConfig.Configure(...);
///.....
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSimpleInjector(_container);
// ....
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
// ....
_container.Verify(VerificationOption.VerifyAndDiagnose);
}
}
Try this:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(new SimpleInjector.Container());
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
}
}
This adds the container to the collection. If you can't retrieve that instance in your test, there is likely something happening that your code doesn't show. As I said, I tried to reproduce the issue, but a call to .Last(d => d.ServiceType == typeof(Container))
results in a returned registration.
Thank you, @dotnetjunkie, your snippet helped.
Btw, I've looked through the sources and haven't found the exact place where SimpleInjector.Container is being added to the IServiceCollection. Can you, please, point me to the place?
@dotnetjunkie Interesting. There is no adding container to IServiceCollection at version 4.8. https://github.com/simpleinjector/SimpleInjector/blob/7c8b3dc653c8115291da8f20272f148eafbdfe15/src/SimpleInjector.Integration.ServiceCollection/SimpleInjectorServiceCollectionExtensions.cs#L65
Probably, somewhere else or this issue has been fixed in a latter version.
Anyway, thanks for help, i really appreciate it.
Ah, that very well could be true. Initially, the container wasn't added. I'm unsure whoch version it was added.
But this means that, as long as you're on 4.8, its best to manually add the container to rhe service collection.
Prerequisites:
I need to replace some of the services registered via Simple Injector for test purposes. It could be easily done using net core built-in DI container. But i found it impossible to implement with Simple Injector.
Default extension points in
WebApplicationFactory
for doing this are either.ConfigureTestServices()
or.ConfigureTestContainer()
.The latter is not an option considering this issue https://github.com/dotnet/aspnetcore/issues/14907
But I couldn't do it via
.ConfigureTestServices()
either.I can not get
Container
instance fromIServiceCollection
(it's simply not there during the.ConfigureTestServices()
).Even if I hack the actual Container Instance from Startup.cs and try to register service mock, I got "Container is locked" error.
So, I'd like to know, if there is a way to use Simple Injector in such cases and, if positive, how do I do that.