.NET 6 Simple Injector has error of the call is ambiguous between SimpleInjectorGenericHostExtensions and SimpleInjectorUseOptionsAspNetCoreExtensions
See original GitHub issueStarted a new .NET 6 web API project today and when implementing Simple Injector similar to a way that is worked in a previous .NET 5 web api project i received the following error when calling app.UseSimpleInjector(container);
Error:
Error CS0121 The call is ambiguous between the following methods or properties: ‘SimpleInjector.SimpleInjectorGenericHostExtensions.UseSimpleInjector(Microsoft.Extensions.Hosting.IHost, SimpleInjector.Container)’ and ‘SimpleInjector.SimpleInjectorUseOptionsAspNetCoreExtensions.UseSimpleInjector(Microsoft.AspNetCore.Builder.IApplicationBuilder, SimpleInjector.Container)’
Project Dependencies:
<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
<PackageReference Include="EFCore.NamingConventions" Version="6.0.0" />
<PackageReference Include="FluentValidation.AspNetCore" Version="10.3.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0" />
<PackageReference Include="RockLib.Logging" Version="3.0.8" />
<PackageReference Include="RockLib.Logging.AspNetCore" Version="3.2.4" />
<PackageReference Include="SimpleInjector.Integration.AspNetCore.Mvc.Core" Version="5.3.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
I’ve tried using the SimpleInjector Package without the Mvc.Core integration but that doesn’t seem to work. I’m unsure where the ambiguity is coming from. Any help would be appreciated.
Program.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SimpleInjector;
using SimpleInjector.Lifestyles;
using Container = SimpleInjector.Container;
var builder = WebApplication.CreateBuilder(args);
IConfiguration configuration = builder.Configuration;
Container container = new Container();
container.Options.DefaultLifestyle = Lifestyle.Scoped;
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
builder.Services.AddMvc();
builder.Services.AddHealthChecks();
builder.Services.AddSimpleInjector(container, options =>
{
options.AddAspNetCore().AddControllerActivation();
options.AddLogging();
});
builder.Services.BaseConfiguration(configuration)
.AddVersioningConfiguration()
.InitializeContainer(configuration, container)
.ConfigureModelBindingExceptionHandling(container)
.AddSwaggerDocumentation()
.AddCustomDbConfiguration(configuration);
var app = builder.Build();
var apiVersionDescriptionProvider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>();
app.UseSimpleInjector(container); // <<< This is what is ambiguious
Issue Analytics
- State:
- Created 2 years ago
- Comments:5

Top Related StackOverflow Question
The new project template for ASP.NET Core 6 contains a ‘simplified’ bootstrapper where
ProgramandStartupclass are merged into a single file. That single file contains no namespaces, no class or method bodies.Below is an example that shows how to integrate Simple Injector in to a ASP.NET Core 6 MVC application, that uses this new template This code is the exact same integration as the example in the documentation:
Change this line:
to the following:
I reflected this in the documentation to allow the documentation to work with all versions of ASP.NET Core.
Let me explain why the call to
app.UseSimpleInjector(container)causes a compile error.The new
Build()method of theWebApplicationBuilderclass has a return type ofWebApplication. ThisWebApplicationis new in .NET 6, and it implements bothIHostandIApplicationBuilder. This is unfortunate, because the Simple Injector integration packages contain helpful extension methods namedUseSimpleInjectorfor bothIHostandIApplicationBuilder. BecauseWebApplicationimplements both interfaces, it causes the C# compiler to show the “ambiguous call” (CS0121) compilation error. The compiler simply can’t decide which extension method to pick (although in practice it wouldn’t make a difference which method it would pick in our case).The problem could be solved by adding -yet another- extension method, but now directly on
WebApplication. BecauseWebApplicationis new in .NET 6, however, it would cause the Simple Injector integration packages to no longer work under earlier releases, which is not an option. That’s why a suggest using the solution above, and this is why I updated the documentation to reflect this.