Files
Umbraco-CMS/src/Umbraco.Web.Common/Builder/UmbracoBuilderExtensions.cs
Andy Butland bdb8f34da3 Netcore: Health check notifier hosted service (#9295)
* Implemented health check notifier as a hosted service.
Added validation to health check settings.

* Registered health check notifier as a hosted service.
Modified health check nested settings to use concrete classes to align with other configuration models.

* Resolved issues with email sending using development server.

* PR review comments and fixed failing unit test.

* Changed period and delay millisecond and hourly values to TimeSpans.
Changed configuration of first run time for health check notifications to use H:mm format.

* Set up SecureSocketOptions as a locally defined enum.

* Tightened up time format validation to verify input is an actual time (with hours and minutes only) and not a timespan.

* Aligned naming and namespace of health check configuration related classes with other configuration classes.

* Created constants for hex colors used in formatting health check results as HTML.

* Revert "Tightened up time format validation to verify input is an actual time (with hours and minutes only) and not a timespan."

This reverts commit f9bb8a7a825bcb58146879f18b47922e09453e2d.

* Renamed method to be clear validation is of a TimeSpan and not a time.

Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2020-10-30 13:56:13 +01:00

87 lines
4.4 KiB
C#

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using Umbraco.Core;
using Umbraco.Extensions;
using Umbraco.Web.Common.Filters;
using Umbraco.Web.Common.ModelBinders;
namespace Umbraco.Web.Common.Builder
{
// TODO: We could add parameters to configure each of these for flexibility
public static class UmbracoBuilderExtensions
{
public static IUmbracoBuilder AddUmbraco(this IServiceCollection services, IWebHostEnvironment webHostEnvironment, IConfiguration config)
{
if (services is null) throw new ArgumentNullException(nameof(services));
if (webHostEnvironment is null) throw new ArgumentNullException(nameof(webHostEnvironment));
if (config is null) throw new ArgumentNullException(nameof(config));
services.AddLazySupport();
var builder = new UmbracoBuilder(services, webHostEnvironment, config);
return builder;
}
public static IUmbracoBuilder WithConfiguration(this IUmbracoBuilder builder)
=> builder.AddWith(nameof(WithConfiguration), () => builder.Services.AddUmbracoConfiguration(builder.Config));
public static IUmbracoBuilder WithCore(this IUmbracoBuilder builder)
=> builder.AddWith(nameof(WithCore), () => builder.Services.AddUmbracoCore(builder.WebHostEnvironment, builder.Config));
public static IUmbracoBuilder WithHostedServices(this IUmbracoBuilder builder)
=> builder.AddWith(nameof(WithHostedServices), () => builder.Services.AddUmbracoHostedServices());
public static IUmbracoBuilder WithMiniProfiler(this IUmbracoBuilder builder)
=> builder.AddWith(nameof(WithMiniProfiler), () =>
builder.Services.AddMiniProfiler(options =>
{
options.ShouldProfile = request => false; // WebProfiler determine and start profiling. We should not use the MiniProfilerMiddleware to also profile
}));
public static IUmbracoBuilder WithMvcAndRazor(this IUmbracoBuilder builder, Action<MvcOptions> mvcOptions = null, Action<IMvcBuilder> mvcBuilding = null)
=> builder.AddWith(nameof(WithMvcAndRazor), () =>
{
// TODO: We need to figure out if we can work around this because calling AddControllersWithViews modifies the global app and order is very important
// this will directly affect developers who need to call that themselves.
//We need to have runtime compilation of views when using umbraco. We could consider having only this when a specific config is set.
//But as far as I can see, there are still precompiled views, even when this is activated, so maybe it is okay.
var mvcBuilder = builder.Services.AddControllersWithViews(options =>
{
options.ModelBinderProviders.Insert(0, new ContentModelBinderProvider());
options.Filters.Insert(0, new EnsurePartialViewMacroViewContextFilterAttribute());
mvcOptions?.Invoke(options);
}).AddRazorRuntimeCompilation();
mvcBuilding?.Invoke(mvcBuilder);
});
public static IUmbracoBuilder WithRuntimeMinifier(this IUmbracoBuilder builder)
=> builder.AddWith(nameof(WithRuntimeMinifier), () => builder.Services.AddUmbracoRuntimeMinifier(builder.Config));
public static IUmbracoBuilder WithWebComponents(this IUmbracoBuilder builder)
=> builder.AddWith(nameof(WithWebComponents), () => builder.Services.AddUmbracoWebComponents());
public static IUmbracoBuilder WithWebServer(this IUmbracoBuilder builder)
=> builder.AddWith(nameof(WithWebServer), () =>
{
// TODO: We need to figure out why thsi is needed and fix those endpoints to not need them, we don't want to change global things
// If using Kestrel: https://stackoverflow.com/a/55196057
builder.Services.Configure<KestrelServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
builder.Services.Configure<IISServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
});
}
}