Closed funkel1989 closed 2 years ago
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 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.
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();
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