How can I configure Wolverine for use with OrchardCore?

129 views Asked by At

I am trying to configure Wolverine in a .NET 7 application that is leveraging the OrchardCore framework. The problem arises from Wolverine swapping out the IoC container, but specifically that it needs to do this with the WebApplicationBuilder.Host property. As soon as it calls routes.MapWolverineEndpoints(), it fails to cast the service provider to a Lamar.IContainer as shown in the exception below.

System.InvalidCastException: 'Unable to cast object of type 'Microsoft.Extensions.DependencyInjection.ServiceProvider' to type 'Lamar.IContainer'.'

Here is the entire Program.cs from an MRE I built to demonstrate the issue:

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);

The MRE repo can be found here.

1

There are 1 answers

4
Jason Pan On

Please change your code like below, and the issue could be fixed.

enter image description here

using Lamar.Microsoft.DependencyInjection;
using Microsoft.OpenApi.Models;
using Oakton;
using Wolverine;
using Wolverine.Http;

var builder = WebApplication.CreateBuilder(args);


builder.Services.AddControllers();
builder.Services.AddOrchardCore();
// Orchard Specific Pipeline
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
    c.EnableAnnotations();
    c.SwaggerDoc("v1", new OpenApiInfo
    {
        Version = "1.0.0",
        Title = "OrchardCore.Mvc.WebApiWothWolverine"
    });
});


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();
}
else
{
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "An ASP.NET Core Web API");
    });
    app.MapWolverineEndpoints();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseOrchardCore();

await app.RunOaktonCommands(args);