Files
Umbraco-CMS/src/Umbraco.Web.Common/Hosting/HostBuilderExtensions.cs
Bjarke Berg b1e42e334d Move to Minimal Hosting Model in a backwards compatible way (#14656)
* Use minimal hosting model

* Make CoreRuntime backward compatible to the old hosting model

* Remove unneccessary methods from interface again

* Pushed the timeout for E2E test to 120 minutes instead of 60

* Updated the preview version from 6 to 7

* Explicitly call BootUmbracoAsync

* Add CreateUmbracoBuilder extension method

* Do not add IRuntime as hosted service when using WebApplication/WebApplicationBuilder

* Set StaticServiceProvider.Instance before booting

* Ensure Umbraco is booted and StaticServiceProvider.Instance is set before configuring middleware

* Do not enable static web assets on production environments

* Removed root namespace from viewImports

---------

Co-authored-by: Andreas Zerbst <andr317c@live.dk>
Co-authored-by: Ronald Barendse <ronald@barend.se>
2023-08-21 12:24:17 +02:00

47 lines
1.6 KiB
C#

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Web.Common.Hosting;
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.Hosting;
/// <summary>
/// Umbraco specific extensions for the <see cref="IHostBuilder" /> interface.
/// </summary>
public static class HostBuilderExtensions
{
/// <summary>
/// Configures an existing <see cref="IHostBuilder" /> with defaults for an Umbraco application.
/// </summary>
public static IHostBuilder ConfigureUmbracoDefaults(this IHostBuilder builder)
=> builder.ConfigureUmbracoDefaults(true);
internal static IHostBuilder ConfigureUmbracoDefaults(this IHostBuilder builder, bool addRuntimeHostedService)
{
#if DEBUG
builder.ConfigureAppConfiguration(config
=> config.AddJsonFile(
"appsettings.Local.json",
true,
true));
#endif
builder.ConfigureLogging(x => x.ClearProviders());
if (addRuntimeHostedService)
{
// Add the Umbraco IRuntime as hosted service
builder.ConfigureServices(services => services.AddHostedService(factory => factory.GetRequiredService<IRuntime>()));
}
return new UmbracoHostBuilderDecorator(builder, OnHostBuilt);
}
// Runs before any IHostedService starts (including generic web host)
private static void OnHostBuilt(IHost host) =>
StaticServiceProvider.Instance = host.Services;
}