Use the new HttpContext.Features API.
This commit is contained in:
parent
7b11506cc0
commit
0bb77764d3
|
|
@ -2,6 +2,7 @@
|
||||||
using Microsoft.AspNet.Builder;
|
using Microsoft.AspNet.Builder;
|
||||||
using Microsoft.AspNet.Diagnostics;
|
using Microsoft.AspNet.Diagnostics;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
|
using Microsoft.AspNet.Http.Features;
|
||||||
using Microsoft.Framework.WebEncoders;
|
using Microsoft.Framework.WebEncoders;
|
||||||
|
|
||||||
namespace ErrorHandlerSample
|
namespace ErrorHandlerSample
|
||||||
|
|
@ -21,7 +22,7 @@ namespace ErrorHandlerSample
|
||||||
await context.Response.WriteAsync("<html><body>\r\n");
|
await context.Response.WriteAsync("<html><body>\r\n");
|
||||||
await context.Response.WriteAsync("We're sorry, we encountered an un-expected issue with your application.<br>\r\n");
|
await context.Response.WriteAsync("We're sorry, we encountered an un-expected issue with your application.<br>\r\n");
|
||||||
|
|
||||||
var error = context.GetFeature<IErrorHandlerFeature>();
|
var error = context.Features.Get<IErrorHandlerFeature>();
|
||||||
if (error != null)
|
if (error != null)
|
||||||
{
|
{
|
||||||
// This error would not normally be exposed to the client
|
// This error would not normally be exposed to the client
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Builder;
|
using Microsoft.AspNet.Builder;
|
||||||
using Microsoft.AspNet.Diagnostics;
|
using Microsoft.AspNet.Diagnostics;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
|
using Microsoft.AspNet.Http.Features;
|
||||||
using Microsoft.Framework.WebEncoders;
|
using Microsoft.Framework.WebEncoders;
|
||||||
|
|
||||||
namespace StatusCodePagesSample
|
namespace StatusCodePagesSample
|
||||||
|
|
@ -40,7 +41,7 @@ namespace StatusCodePagesSample
|
||||||
var disableStatusCodePages = context.Request.Query["disableStatusCodePages"];
|
var disableStatusCodePages = context.Request.Query["disableStatusCodePages"];
|
||||||
if (disableStatusCodePages == "true")
|
if (disableStatusCodePages == "true")
|
||||||
{
|
{
|
||||||
var statusCodePagesFeature = context.GetFeature<IStatusCodePagesFeature>();
|
var statusCodePagesFeature = context.Features.Get<IStatusCodePagesFeature>();
|
||||||
if (statusCodePagesFeature != null)
|
if (statusCodePagesFeature != null)
|
||||||
{
|
{
|
||||||
statusCodePagesFeature.Enabled = false;
|
statusCodePagesFeature.Enabled = false;
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ namespace Microsoft.AspNet.Diagnostics.Elm
|
||||||
{
|
{
|
||||||
using (RequestIdentifier.Ensure(context))
|
using (RequestIdentifier.Ensure(context))
|
||||||
{
|
{
|
||||||
var requestId = context.GetFeature<IHttpRequestIdentifierFeature>().TraceIdentifier;
|
var requestId = context.Features.Get<IHttpRequestIdentifierFeature>().TraceIdentifier;
|
||||||
using (_logger.BeginScope("Request: {RequestId}", requestId))
|
using (_logger.BeginScope("Request: {RequestId}", requestId))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
@ -54,7 +54,7 @@ namespace Microsoft.AspNet.Diagnostics.Elm
|
||||||
{
|
{
|
||||||
return new HttpInfo()
|
return new HttpInfo()
|
||||||
{
|
{
|
||||||
RequestID = context.GetFeature<IHttpRequestIdentifierFeature>().TraceIdentifier,
|
RequestID = context.Features.Get<IHttpRequestIdentifierFeature>().TraceIdentifier,
|
||||||
Host = context.Request.Host,
|
Host = context.Request.Host,
|
||||||
ContentType = context.Request.ContentType,
|
ContentType = context.Request.ContentType,
|
||||||
Path = context.Request.Path,
|
Path = context.Request.Path,
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ namespace Microsoft.AspNet.Diagnostics.Elm
|
||||||
private RequestIdentifier(HttpContext context)
|
private RequestIdentifier(HttpContext context)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
_feature = context.GetFeature<IHttpRequestIdentifierFeature>();
|
_feature = context.Features.Get<IHttpRequestIdentifierFeature>();
|
||||||
|
|
||||||
if (_feature == null)
|
if (_feature == null)
|
||||||
{
|
{
|
||||||
|
|
@ -26,7 +26,7 @@ namespace Microsoft.AspNet.Diagnostics.Elm
|
||||||
{
|
{
|
||||||
TraceIdentifier = Guid.NewGuid().ToString()
|
TraceIdentifier = Guid.NewGuid().ToString()
|
||||||
};
|
};
|
||||||
context.SetFeature(_feature);
|
context.Features.Set(_feature);
|
||||||
_addedFeature = true;
|
_addedFeature = true;
|
||||||
}
|
}
|
||||||
else if (string.IsNullOrEmpty(_feature.TraceIdentifier))
|
else if (string.IsNullOrEmpty(_feature.TraceIdentifier))
|
||||||
|
|
@ -46,7 +46,7 @@ namespace Microsoft.AspNet.Diagnostics.Elm
|
||||||
{
|
{
|
||||||
if (_addedFeature)
|
if (_addedFeature)
|
||||||
{
|
{
|
||||||
_context.SetFeature<IHttpRequestIdentifierFeature>(null);
|
_context.Features.Set<IHttpRequestIdentifierFeature>(null);
|
||||||
}
|
}
|
||||||
else if (_updatedIdentifier)
|
else if (_updatedIdentifier)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ namespace Microsoft.AspNet.Diagnostics
|
||||||
{
|
{
|
||||||
Error = ex,
|
Error = ex,
|
||||||
};
|
};
|
||||||
context.SetFeature<IErrorHandlerFeature>(errorHandlerFeature);
|
context.Features.Set<IErrorHandlerFeature>(errorHandlerFeature);
|
||||||
context.Response.StatusCode = 500;
|
context.Response.StatusCode = 500;
|
||||||
context.Response.Headers.Clear();
|
context.Response.Headers.Clear();
|
||||||
context.Response.OnStarting(_clearCacheHeadersDelegate, context.Response);
|
context.Response.OnStarting(_clearCacheHeadersDelegate, context.Response);
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ using System.Globalization;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Diagnostics;
|
using Microsoft.AspNet.Diagnostics;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
|
using Microsoft.AspNet.Http.Features;
|
||||||
using Microsoft.Framework.Internal;
|
using Microsoft.Framework.Internal;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Builder
|
namespace Microsoft.AspNet.Builder
|
||||||
|
|
@ -126,7 +127,7 @@ namespace Microsoft.AspNet.Builder
|
||||||
|
|
||||||
var originalPath = context.HttpContext.Request.Path;
|
var originalPath = context.HttpContext.Request.Path;
|
||||||
// Store the original paths so the app can check it.
|
// Store the original paths so the app can check it.
|
||||||
context.HttpContext.SetFeature<IStatusCodeReExecuteFeature>(new StatusCodeReExecuteFeature()
|
context.HttpContext.Features.Set<IStatusCodeReExecuteFeature>(new StatusCodeReExecuteFeature()
|
||||||
{
|
{
|
||||||
OriginalPathBase = context.HttpContext.Request.PathBase.Value,
|
OriginalPathBase = context.HttpContext.Request.PathBase.Value,
|
||||||
OriginalPath = originalPath.Value,
|
OriginalPath = originalPath.Value,
|
||||||
|
|
@ -140,7 +141,7 @@ namespace Microsoft.AspNet.Builder
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
context.HttpContext.Request.Path = originalPath;
|
context.HttpContext.Request.Path = originalPath;
|
||||||
context.HttpContext.SetFeature<IStatusCodeReExecuteFeature>(null);
|
context.HttpContext.Features.Set<IStatusCodeReExecuteFeature>(null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Builder;
|
using Microsoft.AspNet.Builder;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
|
using Microsoft.AspNet.Http.Features;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Diagnostics
|
namespace Microsoft.AspNet.Diagnostics
|
||||||
{
|
{
|
||||||
|
|
@ -26,7 +27,7 @@ namespace Microsoft.AspNet.Diagnostics
|
||||||
public async Task Invoke(HttpContext context)
|
public async Task Invoke(HttpContext context)
|
||||||
{
|
{
|
||||||
var statusCodeFeature = new StatusCodePagesFeature();
|
var statusCodeFeature = new StatusCodePagesFeature();
|
||||||
context.SetFeature<IStatusCodePagesFeature>(statusCodeFeature);
|
context.Features.Set<IStatusCodePagesFeature>(statusCodeFeature);
|
||||||
|
|
||||||
await _next(context);
|
await _next(context);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -219,7 +219,7 @@ namespace Microsoft.AspNet.Diagnostics.Tests
|
||||||
.Returns(true);
|
.Returns(true);
|
||||||
var requestIdentifier = new Mock<IHttpRequestIdentifierFeature>();
|
var requestIdentifier = new Mock<IHttpRequestIdentifierFeature>();
|
||||||
requestIdentifier.Setup(f => f.TraceIdentifier).Returns(Guid.NewGuid().ToString());
|
requestIdentifier.Setup(f => f.TraceIdentifier).Returns(Guid.NewGuid().ToString());
|
||||||
contextMock.Setup(c => c.GetFeature<IHttpRequestIdentifierFeature>())
|
contextMock.Setup(c => c.Features.Get<IHttpRequestIdentifierFeature>())
|
||||||
.Returns(requestIdentifier.Object);
|
.Returns(requestIdentifier.Object);
|
||||||
return contextMock;
|
return contextMock;
|
||||||
}
|
}
|
||||||
|
|
@ -236,7 +236,7 @@ namespace Microsoft.AspNet.Diagnostics.Tests
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
var errorPageMiddleware = new ElmCaptureMiddleware((innerContext) =>
|
var errorPageMiddleware = new ElmCaptureMiddleware((innerContext) =>
|
||||||
{
|
{
|
||||||
var feature = innerContext.GetFeature<IHttpRequestIdentifierFeature>();
|
var feature = innerContext.Features.Get<IHttpRequestIdentifierFeature>();
|
||||||
Assert.NotNull(feature);
|
Assert.NotNull(feature);
|
||||||
Assert.False(string.IsNullOrEmpty(feature.TraceIdentifier));
|
Assert.False(string.IsNullOrEmpty(feature.TraceIdentifier));
|
||||||
return Task.FromResult(0);
|
return Task.FromResult(0);
|
||||||
|
|
@ -244,7 +244,7 @@ namespace Microsoft.AspNet.Diagnostics.Tests
|
||||||
|
|
||||||
await errorPageMiddleware.Invoke(context);
|
await errorPageMiddleware.Invoke(context);
|
||||||
|
|
||||||
Assert.Null(context.GetFeature<IHttpRequestIdentifierFeature>());
|
Assert.Null(context.Features.Get<IHttpRequestIdentifierFeature>());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -255,19 +255,19 @@ namespace Microsoft.AspNet.Diagnostics.Tests
|
||||||
{
|
{
|
||||||
TraceIdentifier = Guid.NewGuid().ToString()
|
TraceIdentifier = Guid.NewGuid().ToString()
|
||||||
};
|
};
|
||||||
context.SetFeature<IHttpRequestIdentifierFeature>(requestIdentifierFeature);
|
context.Features.Set<IHttpRequestIdentifierFeature>(requestIdentifierFeature);
|
||||||
var loggerFactory = new LoggerFactory();
|
var loggerFactory = new LoggerFactory();
|
||||||
loggerFactory.AddProvider(new ElmLoggerProvider(new ElmStore(), new ElmOptions()));
|
loggerFactory.AddProvider(new ElmLoggerProvider(new ElmStore(), new ElmOptions()));
|
||||||
|
|
||||||
var errorPageMiddleware = new ElmCaptureMiddleware((innerContext) =>
|
var errorPageMiddleware = new ElmCaptureMiddleware((innerContext) =>
|
||||||
{
|
{
|
||||||
Assert.Same(requestIdentifierFeature, innerContext.GetFeature<IHttpRequestIdentifierFeature>());
|
Assert.Same(requestIdentifierFeature, innerContext.Features.Get<IHttpRequestIdentifierFeature>());
|
||||||
return Task.FromResult(0);
|
return Task.FromResult(0);
|
||||||
}, loggerFactory, new TestElmOptions());
|
}, loggerFactory, new TestElmOptions());
|
||||||
|
|
||||||
await errorPageMiddleware.Invoke(context);
|
await errorPageMiddleware.Invoke(context);
|
||||||
|
|
||||||
Assert.Same(requestIdentifierFeature, context.GetFeature<IHttpRequestIdentifierFeature>());
|
Assert.Same(requestIdentifierFeature, context.Features.Get<IHttpRequestIdentifierFeature>());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
|
@ -277,13 +277,13 @@ namespace Microsoft.AspNet.Diagnostics.Tests
|
||||||
{
|
{
|
||||||
var context = new DefaultHttpContext();
|
var context = new DefaultHttpContext();
|
||||||
var requestIdentifierFeature = new HttpRequestIdentifierFeature() { TraceIdentifier = requestId };
|
var requestIdentifierFeature = new HttpRequestIdentifierFeature() { TraceIdentifier = requestId };
|
||||||
context.SetFeature<IHttpRequestIdentifierFeature>(requestIdentifierFeature);
|
context.Features.Set<IHttpRequestIdentifierFeature>(requestIdentifierFeature);
|
||||||
var loggerFactory = new LoggerFactory();
|
var loggerFactory = new LoggerFactory();
|
||||||
loggerFactory.AddProvider(new ElmLoggerProvider(new ElmStore(), new ElmOptions()));
|
loggerFactory.AddProvider(new ElmLoggerProvider(new ElmStore(), new ElmOptions()));
|
||||||
|
|
||||||
var errorPageMiddleware = new ElmCaptureMiddleware((innerContext) =>
|
var errorPageMiddleware = new ElmCaptureMiddleware((innerContext) =>
|
||||||
{
|
{
|
||||||
var feature = innerContext.GetFeature<IHttpRequestIdentifierFeature>();
|
var feature = innerContext.Features.Get<IHttpRequestIdentifierFeature>();
|
||||||
Assert.NotNull(feature);
|
Assert.NotNull(feature);
|
||||||
Assert.False(string.IsNullOrEmpty(feature.TraceIdentifier));
|
Assert.False(string.IsNullOrEmpty(feature.TraceIdentifier));
|
||||||
return Task.FromResult(0);
|
return Task.FromResult(0);
|
||||||
|
|
@ -291,7 +291,7 @@ namespace Microsoft.AspNet.Diagnostics.Tests
|
||||||
|
|
||||||
await errorPageMiddleware.Invoke(context);
|
await errorPageMiddleware.Invoke(context);
|
||||||
|
|
||||||
Assert.Equal(requestId, context.GetFeature<IHttpRequestIdentifierFeature>().TraceIdentifier);
|
Assert.Equal(requestId, context.Features.Get<IHttpRequestIdentifierFeature>().TraceIdentifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class TestElmOptions : IOptions<ElmOptions>
|
private class TestElmOptions : IOptions<ElmOptions>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue