umbraco / Umbraco-CMS

Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
https://umbraco.com
MIT License
4.42k stars 2.66k forks source link

Umbraco 10 not compatible with minimal hosting model #12445

Closed lauraneto closed 9 months ago

lauraneto commented 2 years ago

Which exact Umbraco version are you using? For example: 9.0.1 - don't just write v9

10.0.0-rc2

Bug summary

When using the minimal hosting model, the backoffice is not accessible in /umbraco.

As a temporary workaround we are forced to manually call app.Services.GetRequiredService<IRuntimeState>().DetermineRuntimeLevel();

Specifics

Umbraco's architecture relies on the execution order of the CoreRuntime hosted service and the web host. It specifically relies on the fact that previously hosted services were started before the web host, which is no longer the case with the new minimal hosting model. As such, when the routes are registered here, the runtime level will still be Unknown.

This is related to https://github.com/dotnet/aspnetcore/issues/39037

Steps to reproduce

Use the following Program.cs:

public class Program
{
    public static void Main(string[] args)
    {
        WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
        builder.Host.ConfigureUmbracoDefaults();
        builder.WebHost.UseWebRoot("wwwroot").UseStaticWebAssets();

        builder.Services.AddUmbraco(builder.Environment, builder.Configuration)
            .AddBackOffice()
            .AddWebsite()
            .AddComposers()
            .Build();

        WebApplication app = builder.Build();

        // Workaround:
        //app.Services.GetRequiredService<IRuntimeState>().DetermineRuntimeLevel();
        //StaticServiceProvider.Instance = app.Services; //While not strictly necessary, it is intended that this gets set now

        if (app.Environment.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseUmbraco()
            .WithMiddleware(u =>
            {
                u.UseBackOffice();
                u.UseWebsite();
            })
            .WithEndpoints(u =>
            {
                u.UseInstallerEndpoints();
                u.UseBackOfficeEndpoints();
                u.UseWebsiteEndpoints();
            });

        app.Run();
    }
}
  1. Try to access the Umbraco backoffice in /umbraco and verify that it doesn't work.

Expected result / actual result

As Umbraco 10 now officially supports ASP.NET Core 6, it should support the minimal hosting model as well. The route registration code should not rely on the CoreRuntime hosted service having ran.

lauraneto commented 2 years ago

The ConfigureUmbracoDefaults method seems to also not have the intended effect when using the minimal hosting model. As it will never set the StaticServiceProvider.Instance

BeardInASuit commented 2 years ago

Based on a working Umbraco 9 minimal host, I only had to add ConfigureUmbracoDefaults() to make it work on .NET6 and Umbraco 10.

If you want to check out a working version that is using a startup class, check out this gist. It might give you some hints.

vsilvar commented 2 years ago

Hi @BeardInASuit By using UseStartup<Startup>() in your example you are functionally opting-out of using the new minimal hosting model. This makes it so you are effectively just using the ASP.NET Core 5 hosting model, and the reason you don't see any issues.

As such, the issue still stands, and the new .NET 6 minimal hosting model should be supported by Umbraco.

ddprince-pro commented 2 years ago

Hey guys,

For anyone looking for a workaround, I did managed to use .NET 6 minimal hosting modal by calling the following just after building the application:

// This is what ConfigureUmbracoDefaults does after building the host and it's required, otherwise it crashes within the Umbraco initialization at startup.
StaticServiceProvider.Instance = app.Services;
// Another workaround due to the nature of the Umbraco initialization strategy. More info over there: https://github.com/umbraco/Umbraco-CMS/issues/12445
app.Services.GetRequiredService<IRuntimeState>().DetermineRuntimeLevel();

Hope this helps anyone!

Zeegaan commented 1 year ago

Just posting an update here: This is wrong by design an is not really a "bug". This is will be a big task, and it will require som investigation from HQ. Sadly no updates on when this will get implemented, but we are investigating it 😊

lauraneto commented 9 months ago

As this is now being released for v13, I will close this! 🙌