如何在基于OrchardCore的.NET 7应用中配置Wolverine?
在OrchardCore + .NET 7应用中配置Wolverine时遇到类型转换异常
我正在基于OrchardCore框架的.NET 7应用中配置Wolverine,问题在于Wolverine需要替换IoC容器,且必须通过WebApplicationBuilder.Host属性完成该操作。但调用routes.MapWolverineEndpoints()时,抛出了如下类型转换异常:
System.InvalidCastException: 'Unable to cast object of type 'Microsoft.Extensions.DependencyInjection.ServiceProvider' to type 'Lamar.IContainer'.'
以下是用于复现问题的最小可复现示例(MRE)中的完整Program.cs代码:
using Microsoft.OpenApi.Models; using Oakton; using Wolverine; using Wolverine.Http; var builder = WebApplication.CreateBuilder(args); builder.Services .AddOrchardCore() .AddMvc() // Orchard Specific Pipeline .ConfigureServices( services => { services.AddEndpointsApiExplorer(); services.AddSwaggerGen(c => { c.EnableAnnotations(); c.SwaggerDoc("v1", new OpenApiInfo { Version = "1.0.0", Title = "OrchardCore.Mvc.WebApiWothWolverine" }); }); }) .Configure( (app, routes, services) => { routes.MapWolverineEndpoints(); var env = app.ApplicationServices.GetService<IWebHostEnvironment>(); env ??= app.ApplicationServices.GetService<IHostEnvironment>() as IWebHostEnvironment; if (env?.IsDevelopment() ?? false) { app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "An ASP.NET Core Web API"); }); } }); builder.Host.UseWolverine(); var app = builder.Build(); if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseOrchardCore(); await app.RunOaktonCommands(args);
内容的提问来源于stack exchange,提问作者Mark Clark
相关产品推荐
相关产品推荐

