Error message on no dispatcher middleware in endpoint middleware (#600)

This commit is contained in:
James Newton-King 2018-07-13 10:16:21 +12:00 committed by GitHub
parent 3a022107dc
commit 0cf972cc43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 0 deletions

View File

@ -32,6 +32,11 @@ namespace Microsoft.AspNetCore.Routing
public async Task Invoke(HttpContext httpContext)
{
var feature = httpContext.Features.Get<IEndpointFeature>();
if (feature == null)
{
throw new InvalidOperationException("Unable to execute an endpoint because the dispatcher was not run. Ensure dispatcher middleware is registered.");
}
if (feature.Invoker != null)
{
Log.ExecutingEndpoint(_logger, feature.Endpoint);

View File

@ -0,0 +1,48 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Logging.Testing;
using Xunit;
namespace Microsoft.AspNetCore.Routing
{
public class EndpointMiddlewareTest
{
[Fact]
public async Task Invoke_NoFeature_ThrowFriendlyErrorMessage()
{
// Arrange
var httpContext = new DefaultHttpContext();
httpContext.RequestServices = new ServiceProvider();
RequestDelegate next = (c) =>
{
return Task.FromResult<object>(null);
};
var middleware = new EndpointMiddleware(NullLogger<EndpointMiddleware>.Instance, next);
// Act
var invokeTask = middleware.Invoke(httpContext);
// Assert
var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () => await invokeTask);
Assert.Equal("Unable to execute an endpoint because the dispatcher was not run. Ensure dispatcher middleware is registered.", ex.Message);
}
private class ServiceProvider : IServiceProvider
{
public object GetService(Type serviceType)
{
throw new NotImplementedException();
}
}
}
}