-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.NET 6 Simple Injector has error of the call is ambiguous between SimpleInjectorGenericHostExtensions and SimpleInjectorUseOptionsAspNetCoreExtensions #933
Comments
So it looks like in .NET 6 builder.Build() Returns a Type of WebApplication instead of IApplicationBuilder. WebApplication must contain other extension methods that conflict with SimpleInjector? I fixed this by abstraction app.UseCors into an extension method that returned IApplicationBuilder and than chained UseSimpleIngector(Container); to the end of that. Still working through other issues before I can determine if this works as a workaround but it compiles. See below for code. UseCustomCors: public static IApplicationBuilder UseCustomCors(this IApplicationBuilder app)
{
app.UseCors(Constants.CorPolicyName);
return app;
} Program.cs var app = builder.Build();
var apiVersionDescriptionProvider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>();
app.UseCustomCors().UseSimpleInjector(container); One thing to note, if you do IApplicationBuilder app = builder.Build(); this will work for everything still but removes the ability to do the app.Services.GetRequiredService that I need to do using this new .NET 6 Pattern. |
Can confirm. The above resolves the problem and everything seems to function as expected but you should consider this issue as a bug IMO as this isn't a workaround someone should have to do. |
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 The new The problem could be solved by adding -yet another- extension method, but now directly on |
The new project template for ASP.NET Core 6 contains a 'simplified' bootstrapper where 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(); |
Thanks a lot for the information this post provides. This works great! |
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:
Project Dependencies:
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
The text was updated successfully, but these errors were encountered: