I've been looking through the Simple Injector documentation and thought I was doing things right regarding Crosswiring, but alas...
I have the following which fails to resolve my App through the IServiceProvider
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SimpleInjector;
using SimpleInjector.Lifestyles;
using SimpleInjectorTesting;
Container container = new Container { Options = { DefaultScopedLifestyle =
new AsyncScopedLifestyle()}};
using IHost host = Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Information))
.ConfigureServices((hostBuilderContext, services) =>
{
container.RegisterSingleton<App>();
services.AddSingleton<ISaySomethingService, SaySomethingService>();
services.AddSimpleInjector(container);
})
.Build();
host.Services.UseSimpleInjector(container);
container.Verify();
var app = host.Services.GetRequiredService<App>();
//var app = container.GetInstance<App>();
await app.Run();
If I switch to use the container.GetInstance<App>()
call the it works fine, SI can resolve services registered from IServiceCollection
.
If I set options.AddLogging()
to SI, an IServiceProvider
registered type can't receive ILogger even if I'm using container.GetInstance<App>()
System.InvalidOperationException: 'The configuration is invalid. Creating the instance for type App failed. The registered delegate for type ISaySomethingService threw an exception. Failed to resolve ISaySomethingService. ISaySomethingService is a cross-wired service, meaning that Simple Injector forwarded the request the framework's IServiceProvider in order to get an instance. The used Microsoft.Extensions.DependencyInjection.ServiceProvider, however, failed with the following message: "Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while attempting to activate 'SimpleInjectorTesting.SaySomethingService'.". This error might indicate a misconfiguration of services in the framework's IServiceCollection.'
So I have this library that does a bunch of stuff using SI and leverages decorators, conditionals, etc that standard .NET DI makes difficult but I want this library to be usable by anyone who may not be using Simple Injector. I have an extension method to Register my framework and then another to use it which does the container.Verify()
call to get the SI specific stuff tucked away. But then I can't resolve through IServiceProvider
as per the code above. Is this even possible with SI and if so what am I missing?
Update: This is my App class
public class App
{
private readonly ISaySomethingService _saySomethingService;
public App(ISaySomethingService saySomethingService)
{
_saySomethingService = saySomethingService;
}
public async Task Run(CancellationToken cancellationToken = default)
{
Console.WriteLine(_saySomethingService.Message());
}
}
ISaySomethingService
in theIServiceCollection
instead of registering it in Simple Injector?container.RegisterSingleton<ISaySomethingService, SaySomethingService>();
I still getSystem.InvalidOperationException: 'No service for type 'SimpleInjectorTesting.App' has been registered.'
when I try to resolve App from IServiceProvider withvar app = host.Services.GetRequiredService<App>()
. Is that just not doable?