Add friendly error message for when healthchecks services not registered (#8146)
This commit is contained in:
parent
e4f5fef7ac
commit
21b20afb1d
|
|
@ -4,6 +4,8 @@
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Builder
|
namespace Microsoft.AspNetCore.Builder
|
||||||
|
|
@ -225,6 +227,14 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
|
|
||||||
private static void UseHealthChecksCore(IApplicationBuilder app, PathString path, int? port, object[] args)
|
private static void UseHealthChecksCore(IApplicationBuilder app, PathString path, int? port, object[] args)
|
||||||
{
|
{
|
||||||
|
if (app.ApplicationServices.GetService(typeof(HealthCheckService)) == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException(Resources.FormatUnableToFindServices(
|
||||||
|
nameof(IServiceCollection),
|
||||||
|
nameof(HealthCheckServiceCollectionExtensions.AddHealthChecks),
|
||||||
|
"ConfigureServices(...)"));
|
||||||
|
}
|
||||||
|
|
||||||
// NOTE: we explicitly don't use Map here because it's really common for multiple health
|
// NOTE: we explicitly don't use Map here because it's really common for multiple health
|
||||||
// check middleware to overlap in paths. Ex: `/health`, `/health/detailed` - this is order
|
// check middleware to overlap in paths. Ex: `/health`, `/health/detailed` - this is order
|
||||||
// sensititive with Map, and it's really surprising to people.
|
// sensititive with Map, and it's really surprising to people.
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Microsoft.AspNetCore.Routing;
|
using Microsoft.AspNetCore.Routing;
|
||||||
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Builder
|
namespace Microsoft.AspNetCore.Builder
|
||||||
{
|
{
|
||||||
|
|
@ -93,6 +95,14 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
|
|
||||||
private static IEndpointConventionBuilder MapHealthChecksCore(IEndpointRouteBuilder builder, string pattern, HealthCheckOptions options, string displayName)
|
private static IEndpointConventionBuilder MapHealthChecksCore(IEndpointRouteBuilder builder, string pattern, HealthCheckOptions options, string displayName)
|
||||||
{
|
{
|
||||||
|
if (builder.ServiceProvider.GetService(typeof(HealthCheckService)) == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException(Resources.FormatUnableToFindServices(
|
||||||
|
nameof(IServiceCollection),
|
||||||
|
nameof(HealthCheckServiceCollectionExtensions.AddHealthChecks),
|
||||||
|
"ConfigureServices(...)"));
|
||||||
|
}
|
||||||
|
|
||||||
var args = options != null ? new[] { Options.Create(options) } : Array.Empty<object>();
|
var args = options != null ? new[] { Options.Create(options) } : Array.Empty<object>();
|
||||||
|
|
||||||
var pipeline = builder.CreateApplicationBuilder()
|
var pipeline = builder.CreateApplicationBuilder()
|
||||||
|
|
|
||||||
|
|
@ -24,4 +24,11 @@
|
||||||
<Reference Include="Microsoft.Net.Http.Headers" />
|
<Reference Include="Microsoft.Net.Http.Headers" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<EmbeddedResource Update="Resources.resx">
|
||||||
|
<Generator>ResXFileCodeGenerator</Generator>
|
||||||
|
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
||||||
|
</EmbeddedResource>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,44 @@
|
||||||
|
// <auto-generated />
|
||||||
|
namespace Microsoft.AspNetCore.Diagnostics.HealthChecks
|
||||||
|
{
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Resources;
|
||||||
|
|
||||||
|
internal static class Resources
|
||||||
|
{
|
||||||
|
private static readonly ResourceManager _resourceManager
|
||||||
|
= new ResourceManager("Microsoft.AspNetCore.Diagnostics.HealthChecks.Resources", typeof(Resources).GetTypeInfo().Assembly);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Unable to find the required services. Please add all the required services by calling '{0}.{1}' inside the call to '{2}' in the application startup code.
|
||||||
|
/// </summary>
|
||||||
|
internal static string UnableToFindServices
|
||||||
|
{
|
||||||
|
get => GetString("UnableToFindServices");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Unable to find the required services. Please add all the required services by calling '{0}.{1}' inside the call to '{2}' in the application startup code.
|
||||||
|
/// </summary>
|
||||||
|
internal static string FormatUnableToFindServices(object p0, object p1, object p2)
|
||||||
|
=> string.Format(CultureInfo.CurrentCulture, GetString("UnableToFindServices"), p0, p1, p2);
|
||||||
|
|
||||||
|
private static string GetString(string name, params string[] formatterNames)
|
||||||
|
{
|
||||||
|
var value = _resourceManager.GetString(name);
|
||||||
|
|
||||||
|
System.Diagnostics.Debug.Assert(value != null);
|
||||||
|
|
||||||
|
if (formatterNames != null)
|
||||||
|
{
|
||||||
|
for (var i = 0; i < formatterNames.Length; i++)
|
||||||
|
{
|
||||||
|
value = value.Replace("{" + formatterNames[i] + "}", "{" + i + "}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,123 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<root>
|
||||||
|
<!--
|
||||||
|
Microsoft ResX Schema
|
||||||
|
|
||||||
|
Version 2.0
|
||||||
|
|
||||||
|
The primary goals of this format is to allow a simple XML format
|
||||||
|
that is mostly human readable. The generation and parsing of the
|
||||||
|
various data types are done through the TypeConverter classes
|
||||||
|
associated with the data types.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
... ado.net/XML headers & schema ...
|
||||||
|
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||||
|
<resheader name="version">2.0</resheader>
|
||||||
|
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||||
|
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||||
|
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||||
|
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||||
|
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||||
|
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||||
|
</data>
|
||||||
|
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||||
|
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||||
|
<comment>This is a comment</comment>
|
||||||
|
</data>
|
||||||
|
|
||||||
|
There are any number of "resheader" rows that contain simple
|
||||||
|
name/value pairs.
|
||||||
|
|
||||||
|
Each data row contains a name, and value. The row also contains a
|
||||||
|
type or mimetype. Type corresponds to a .NET class that support
|
||||||
|
text/value conversion through the TypeConverter architecture.
|
||||||
|
Classes that don't support this are serialized and stored with the
|
||||||
|
mimetype set.
|
||||||
|
|
||||||
|
The mimetype is used for serialized objects, and tells the
|
||||||
|
ResXResourceReader how to depersist the object. This is currently not
|
||||||
|
extensible. For a given mimetype the value must be set accordingly:
|
||||||
|
|
||||||
|
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||||
|
that the ResXResourceWriter will generate, however the reader can
|
||||||
|
read any of the formats listed below.
|
||||||
|
|
||||||
|
mimetype: application/x-microsoft.net.object.binary.base64
|
||||||
|
value : The object must be serialized with
|
||||||
|
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
||||||
|
: and then encoded with base64 encoding.
|
||||||
|
|
||||||
|
mimetype: application/x-microsoft.net.object.soap.base64
|
||||||
|
value : The object must be serialized with
|
||||||
|
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||||
|
: and then encoded with base64 encoding.
|
||||||
|
|
||||||
|
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||||
|
value : The object must be serialized into a byte array
|
||||||
|
: using a System.ComponentModel.TypeConverter
|
||||||
|
: and then encoded with base64 encoding.
|
||||||
|
-->
|
||||||
|
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||||
|
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||||
|
<xsd:element name="root" msdata:IsDataSet="true">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:choice maxOccurs="unbounded">
|
||||||
|
<xsd:element name="metadata">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:sequence>
|
||||||
|
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||||
|
</xsd:sequence>
|
||||||
|
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||||
|
<xsd:attribute name="type" type="xsd:string" />
|
||||||
|
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||||
|
<xsd:attribute ref="xml:space" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
<xsd:element name="assembly">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:attribute name="alias" type="xsd:string" />
|
||||||
|
<xsd:attribute name="name" type="xsd:string" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
<xsd:element name="data">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:sequence>
|
||||||
|
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||||
|
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||||
|
</xsd:sequence>
|
||||||
|
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||||
|
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||||
|
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||||
|
<xsd:attribute ref="xml:space" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
<xsd:element name="resheader">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:sequence>
|
||||||
|
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||||
|
</xsd:sequence>
|
||||||
|
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
</xsd:choice>
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
</xsd:schema>
|
||||||
|
<resheader name="resmimetype">
|
||||||
|
<value>text/microsoft-resx</value>
|
||||||
|
</resheader>
|
||||||
|
<resheader name="version">
|
||||||
|
<value>2.0</value>
|
||||||
|
</resheader>
|
||||||
|
<resheader name="reader">
|
||||||
|
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
|
</resheader>
|
||||||
|
<resheader name="writer">
|
||||||
|
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
|
</resheader>
|
||||||
|
<data name="UnableToFindServices" xml:space="preserve">
|
||||||
|
<value>Unable to find the required services. Please add all the required services by calling '{0}.{1}' inside the call to '{2}' in the application startup code.</value>
|
||||||
|
</data>
|
||||||
|
</root>
|
||||||
|
|
@ -18,6 +18,31 @@ namespace Microsoft.AspNetCore.Diagnostics.HealthChecks
|
||||||
{
|
{
|
||||||
public class HealthCheckEndpointRouteBuilderExtensionsTest
|
public class HealthCheckEndpointRouteBuilderExtensionsTest
|
||||||
{
|
{
|
||||||
|
[Fact]
|
||||||
|
public void ThrowFriendlyErrorWhenServicesNotRegistered()
|
||||||
|
{
|
||||||
|
var builder = new WebHostBuilder()
|
||||||
|
.Configure(app =>
|
||||||
|
{
|
||||||
|
app.UseRouting(routes =>
|
||||||
|
{
|
||||||
|
routes.MapHealthChecks("/healthz");
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.ConfigureServices(services =>
|
||||||
|
{
|
||||||
|
services.AddRouting();
|
||||||
|
});
|
||||||
|
|
||||||
|
var ex = Assert.Throws<InvalidOperationException>(() => new TestServer(builder));
|
||||||
|
|
||||||
|
Assert.Equal(
|
||||||
|
"Unable to find the required services. Please add all the required services by calling " +
|
||||||
|
"'IServiceCollection.AddHealthChecks' inside the call to 'ConfigureServices(...)' " +
|
||||||
|
"in the application startup code.",
|
||||||
|
ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task MapHealthChecks_ReturnsOk()
|
public async Task MapHealthChecks_ReturnsOk()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,24 @@ namespace Microsoft.AspNetCore.Diagnostics.HealthChecks
|
||||||
{
|
{
|
||||||
public class HealthCheckMiddlewareTests
|
public class HealthCheckMiddlewareTests
|
||||||
{
|
{
|
||||||
|
[Fact]
|
||||||
|
public void ThrowFriendlyErrorWhenServicesNotRegistered()
|
||||||
|
{
|
||||||
|
var builder = new WebHostBuilder()
|
||||||
|
.Configure(app =>
|
||||||
|
{
|
||||||
|
app.UseHealthChecks("/health");
|
||||||
|
});
|
||||||
|
|
||||||
|
var ex = Assert.Throws<InvalidOperationException>(() => new TestServer(builder));
|
||||||
|
|
||||||
|
Assert.Equal(
|
||||||
|
"Unable to find the required services. Please add all the required services by calling " +
|
||||||
|
"'IServiceCollection.AddHealthChecks' inside the call to 'ConfigureServices(...)' " +
|
||||||
|
"in the application startup code.",
|
||||||
|
ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact] // Matches based on '.Map'
|
[Fact] // Matches based on '.Map'
|
||||||
public async Task IgnoresRequestThatDoesNotMatchPath()
|
public async Task IgnoresRequestThatDoesNotMatchPath()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue