question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.NET 6 Simple Injector has error of the call is ambiguous between SimpleInjectorGenericHostExtensions and SimpleInjectorUseOptionsAspNetCoreExtensions

See original GitHub issue

Started 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:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

5reactions
dotnetjunkiecommented, Dec 1, 2021

The new project template for ASP.NET Core 6 contains a ‘simplified’ bootstrapper where Program and Startup class 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:

// Program.cs
// Used NuGet Packages: SimpleInjector + SimpleInjector.Integration.AspNetCore.Mvc
using SimpleInjector;

var container = new Container();

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
var services = builder.Services;

services.AddControllersWithViews();

services.AddLogging();
services.AddLocalization(options => options.ResourcesPath = "Resources");

services.AddSimpleInjector(container, options =>
{
    options.AddAspNetCore()
        .AddControllerActivation()
        .AddViewComponentActivation()
        .AddPageModelActivation()
        .AddTagHelperActivation();

    options.AddLogging();
    options.AddLocalization();
});

InitializeContainer();

void InitializeContainer()
{
    container.Register<IUserService, UserService>(Lifestyle.Singleton);
}

WebApplication app = builder.Build();

app.Services.UseSimpleInjector(container);

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.UseMiddleware<CustomMiddleware1>(container);
app.UseMiddleware<CustomMiddleware2>(container);

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

container.Verify();

app.Run();
1reaction
dotnetjunkiecommented, Dec 2, 2021

Change this line:

app.UseSimpleInjector(container);

to the following:

app.Services.UseSimpleInjector(container);

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 the WebApplicationBuilder class has a return type of WebApplication. This WebApplication is new in .NET 6, and it implements both IHost and IApplicationBuilder. This is unfortunate, because the Simple Injector integration packages contain helpful extension methods named UseSimpleInjector for both IHost and IApplicationBuilder. Because WebApplication implements 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. Because WebApplication is 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Welcome to Simple Injector's documentation! — Simple ...
Simple Injector is an easy-to-use Dependency Injection (DI) library for .NET that supports .NET Core, Xamarin, Mono and Universal apps.
Read more >
Asp.NET Core with SimpleInjector Instance registration error
Ok I see now. However the controller is located in another Assembly so the URL is always not found. How can I register...
Read more >
How to use Simple Injector in ASP.NET Core MVC
Simple Injector is a free, fast, and flexible inversion of control library that is easy to use and configure. It supports .NET Core,...
Read more >
Dependency Injection With Simple Injector in .NET
Simple Injector is an open-source .NET library that enables developers to apply dependency injection into their code.
Read more >
Chapter 14: The Simple Injector DI Container
Primitive types can't be registered directly to prevent registrations from becoming ambiguous. Object graphs can be verified to spot common configuration errors ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found