Closed eerhardt closed 3 years ago
Here's the issue without orchard:
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var builder = WebApplication.CreateBuilder(args);
// Add the service collection to the service collection
builder.Services.AddSingleton(builder.Services);
var app = builder.Build();
// This works fine
var env = app.Services.GetRequiredService<IHostEnvironment>();
// This will go boom because the IHostEnvironment wasn't in the IServiceCollection
env = app.Services.GetRequiredService<IServiceCollection>().BuildServiceProvider().GetRequiredService<IHostEnvironment>();
Typically the IHostEnvironment is added by the HostBuilder this happens when you build. Using the callback model we get access to all of the services because we run user code after adding these defaults but we don't get access to them with the minimal host.
@halter73 this is the same problem you brought up earlier, looks like orchard hits that edge case and its no longer an edge case 😄
Looking at the slew of services here it seems the ones you'd want to access in this edgecasey way is IHostEnviroment and IHostApplicationLifetime
Describe the bug
When trying to use the new minimal Main method pattern in ASP.NET 6 and loading OrchardCore, I'm getting an exception that the IHostEnvironment service is not available.
To Reproduce
Notes
Note that if I write the Main method using the configure lambda pattern in https://github.com/dotnet/aspnetcore/issues/34208, that this exception doesn't occur.
Exceptions (if any)
Further technical details
dotnet --info
Runtime Environment: OS Name: Windows OS Version: 10.0.19043 OS Platform: Windows RID: win10-x64 Base Path: C:\dotnet\sdk\6.0.100-preview.7.21357.29\
Host (useful for support): Version: 6.0.0-preview.7.21356.2 Commit: 566b53a66b
.NET SDKs installed: 6.0.100-preview.7.21357.29 [C:\dotnet\sdk]
.NET runtimes installed: Microsoft.AspNetCore.App 6.0.0-preview.7.21355.4 [C:\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.0-preview.7.21356.2 [C:\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 6.0.0-preview.7.21352.1 [C:\dotnet\shared\Microsoft.WindowsDesktop.App]
To install additional .NET runtimes or SDKs: https://aka.ms/dotnet-download