React to IContextAccessor -> IScopedInstance

This commit is contained in:
Hao Kung 2015-01-12 10:25:21 -08:00
parent f8cb519c2f
commit 157b633758
14 changed files with 43 additions and 56 deletions

View File

@ -24,7 +24,7 @@ namespace Microsoft.AspNet.Mvc
public AntiForgeryToken GetCookieToken(HttpContext httpContext) public AntiForgeryToken GetCookieToken(HttpContext httpContext)
{ {
var contextAccessor = var contextAccessor =
httpContext.RequestServices.GetRequiredService<IContextAccessor<AntiForgeryContext>>(); httpContext.RequestServices.GetRequiredService<IScopedInstance<AntiForgeryContext>>();
if (contextAccessor.Value != null) if (contextAccessor.Value != null)
{ {
return contextAccessor.Value.CookieToken; return contextAccessor.Value.CookieToken;
@ -58,9 +58,9 @@ namespace Microsoft.AspNet.Mvc
// Add the cookie to the request based context. // Add the cookie to the request based context.
// This is useful if the cookie needs to be reloaded in the context of the same request. // This is useful if the cookie needs to be reloaded in the context of the same request.
var contextAccessor = var contextAccessor =
httpContext.RequestServices.GetRequiredService<IContextAccessor<AntiForgeryContext>>(); httpContext.RequestServices.GetRequiredService<IScopedInstance<AntiForgeryContext>>();
Debug.Assert(contextAccessor.Value == null, "AntiForgeryContext should be set only once per request."); Debug.Assert(contextAccessor.Value == null, "AntiForgeryContext should be set only once per request.");
contextAccessor.SetValue(new AntiForgeryContext() { CookieToken = token }); contextAccessor.Value = new AntiForgeryContext() { CookieToken = token };
var serializedToken = _serializer.Serialize(token); var serializedToken = _serializer.Serialize(token);
var options = new CookieOptions() { HttpOnly = true }; var options = new CookieOptions() { HttpOnly = true };

View File

@ -19,7 +19,7 @@ namespace Microsoft.AspNet.Mvc
private readonly IBodyModelValidator _bodyModelValidator; private readonly IBodyModelValidator _bodyModelValidator;
private readonly IValidationExcludeFiltersProvider _bodyValidationExcludeFiltersProvider; private readonly IValidationExcludeFiltersProvider _bodyValidationExcludeFiltersProvider;
public BodyModelBinder([NotNull] IContextAccessor<ActionContext> context, public BodyModelBinder([NotNull] IScopedInstance<ActionContext> context,
[NotNull] IInputFormatterSelector selector, [NotNull] IInputFormatterSelector selector,
[NotNull] IBodyModelValidator bodyModelValidator, [NotNull] IBodyModelValidator bodyModelValidator,
[NotNull] IValidationExcludeFiltersProvider bodyValidationExcludeFiltersProvider) [NotNull] IValidationExcludeFiltersProvider bodyValidationExcludeFiltersProvider)

View File

@ -95,32 +95,25 @@ namespace Microsoft.AspNet.Mvc
var optionsAccessor = services.GetRequiredService<IOptions<MvcOptions>>(); var optionsAccessor = services.GetRequiredService<IOptions<MvcOptions>>();
actionContext.ModelState.MaxAllowedErrors = optionsAccessor.Options.MaxModelValidationErrors; actionContext.ModelState.MaxAllowedErrors = optionsAccessor.Options.MaxModelValidationErrors;
var contextAccessor = services.GetRequiredService<IContextAccessor<ActionContext>>(); var contextAccessor = services.GetRequiredService<IScopedInstance<ActionContext>>();
using (contextAccessor.SetContextSource(() => actionContext, PreventExchange)) contextAccessor.Value = actionContext;
var invokerFactory = services.GetRequiredService<IActionInvokerFactory>();
var invoker = invokerFactory.CreateInvoker(actionContext);
if (invoker == null)
{ {
var invokerFactory = services.GetRequiredService<IActionInvokerFactory>(); LogActionSelection(actionSelected: true, actionInvoked: false, handled: context.IsHandled);
var invoker = invokerFactory.CreateInvoker(actionContext);
if (invoker == null)
{
LogActionSelection(actionSelected: true, actionInvoked: false, handled: context.IsHandled);
var ex = new InvalidOperationException( var ex = new InvalidOperationException(
Resources.FormatActionInvokerFactory_CouldNotCreateInvoker( Resources.FormatActionInvokerFactory_CouldNotCreateInvoker(
actionDescriptor.DisplayName)); actionDescriptor.DisplayName));
// Add tracing/logging (what do we think of this pattern of // Add tracing/logging (what do we think of this pattern of
// tacking on extra data on the exception?) // tacking on extra data on the exception?)
ex.Data.Add("AD", actionDescriptor); ex.Data.Add("AD", actionDescriptor);
throw ex; throw ex;
}
await invoker.InvokeAsync();
} }
}
private ActionContext PreventExchange(ActionContext contex) await invoker.InvokeAsync();
{
throw new InvalidOperationException(Resources.ActionContextAccessor_SetValueNotSupported);
} }
private void EnsureLogger(HttpContext context) private void EnsureLogger(HttpContext context)

View File

@ -30,7 +30,7 @@ namespace Microsoft.AspNet.Mvc
/// <param name="actionSelector">The <see cref="IActionSelector"/> to be used for verifying the correctness of /// <param name="actionSelector">The <see cref="IActionSelector"/> to be used for verifying the correctness of
/// supplied parameters for a route. /// supplied parameters for a route.
/// </param> /// </param>
public UrlHelper(IContextAccessor<ActionContext> contextAccessor, IActionSelector actionSelector) public UrlHelper(IScopedInstance<ActionContext> contextAccessor, IActionSelector actionSelector)
{ {
_httpContext = contextAccessor.Value.HttpContext; _httpContext = contextAccessor.Value.HttpContext;
_router = contextAccessor.Value.RouteData.Routers[0]; _router = contextAccessor.Value.RouteData.Routers[0];

View File

@ -22,7 +22,7 @@ namespace Microsoft.Framework.DependencyInjection
services.AddOptions(configuration); services.AddOptions(configuration);
services.AddDataProtection(configuration); services.AddDataProtection(configuration);
services.AddRouting(configuration); services.AddRouting(configuration);
services.AddContextAccessor(configuration); services.AddScopedInstance(configuration);
services.Configure<RouteOptions>(routeOptions => services.Configure<RouteOptions>(routeOptions =>
routeOptions.ConstraintMap routeOptions.ConstraintMap
.Add("exists", .Add("exists",

View File

@ -116,7 +116,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test
{ {
var httpContext = new Mock<HttpContext>(); var httpContext = new Mock<HttpContext>();
var actionContext = GetActionContext(httpContext.Object); var actionContext = GetActionContext(httpContext.Object);
var mockContentAccessor = new Mock<IContextAccessor<ActionContext>>(); var mockContentAccessor = new Mock<IScopedInstance<ActionContext>>();
mockContentAccessor.SetupGet(o => o.Value).Returns(actionContext); mockContentAccessor.SetupGet(o => o.Value).Returns(actionContext);
var mockActionSelector = new Mock<IActionSelector>(); var mockActionSelector = new Mock<IActionSelector>();
var urlHelper = new UrlHelper(mockContentAccessor.Object, mockActionSelector.Object); var urlHelper = new UrlHelper(mockContentAccessor.Object, mockActionSelector.Object);

View File

@ -29,7 +29,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test
mockHttpContext mockHttpContext
.Setup(o => o.Request.Cookies) .Setup(o => o.Request.Cookies)
.Returns(requestCookies.Object); .Returns(requestCookies.Object);
var contextAccessor = new ContextAccessor<AntiForgeryContext>(); var contextAccessor = new ScopedInstance<AntiForgeryContext>();
mockHttpContext.SetupGet(o => o.RequestServices) mockHttpContext.SetupGet(o => o.RequestServices)
.Returns(GetServiceProvider(contextAccessor)); .Returns(GetServiceProvider(contextAccessor));
var config = new AntiForgeryOptions() var config = new AntiForgeryOptions()
@ -60,13 +60,13 @@ namespace Microsoft.AspNet.Mvc.Core.Test
mockHttpContext mockHttpContext
.Setup(o => o.Request.Cookies) .Setup(o => o.Request.Cookies)
.Returns(requestCookies.Object); .Returns(requestCookies.Object);
var contextAccessor = new ContextAccessor<AntiForgeryContext>(); var contextAccessor = new ScopedInstance<AntiForgeryContext>();
mockHttpContext.SetupGet(o => o.RequestServices) mockHttpContext.SetupGet(o => o.RequestServices)
.Returns(GetServiceProvider(contextAccessor)); .Returns(GetServiceProvider(contextAccessor));
// add a cookie explicitly. // add a cookie explicitly.
var cookie = new AntiForgeryToken(); var cookie = new AntiForgeryToken();
contextAccessor.SetValue(new AntiForgeryContext() { CookieToken = cookie }); contextAccessor.Value = new AntiForgeryContext() { CookieToken = cookie };
var config = new AntiForgeryOptions() var config = new AntiForgeryOptions()
{ {
CookieName = _cookieName CookieName = _cookieName
@ -275,7 +275,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test
var mockHttpContext = new Mock<HttpContext>(); var mockHttpContext = new Mock<HttpContext>();
mockHttpContext.Setup(o => o.Response.Cookies) mockHttpContext.Setup(o => o.Response.Cookies)
.Returns(cookies); .Returns(cookies);
var contextAccessor = new ContextAccessor<AntiForgeryContext>(); var contextAccessor = new ScopedInstance<AntiForgeryContext>();
mockHttpContext.SetupGet(o => o.RequestServices) mockHttpContext.SetupGet(o => o.RequestServices)
.Returns(GetServiceProvider(contextAccessor)); .Returns(GetServiceProvider(contextAccessor));
@ -317,17 +317,17 @@ namespace Microsoft.AspNet.Mvc.Core.Test
mockHttpContext.Setup(o => o.Request) mockHttpContext.Setup(o => o.Request)
.Returns(request.Object); .Returns(request.Object);
var contextAccessor = new ContextAccessor<AntiForgeryContext>(); var contextAccessor = new ScopedInstance<AntiForgeryContext>();
mockHttpContext.SetupGet(o => o.RequestServices) mockHttpContext.SetupGet(o => o.RequestServices)
.Returns(GetServiceProvider(contextAccessor)); .Returns(GetServiceProvider(contextAccessor));
return mockHttpContext.Object; return mockHttpContext.Object;
} }
private static IServiceProvider GetServiceProvider(IContextAccessor<AntiForgeryContext> contextAccessor) private static IServiceProvider GetServiceProvider(IScopedInstance<AntiForgeryContext> contextAccessor)
{ {
var serviceCollection = new ServiceCollection(); var serviceCollection = new ServiceCollection();
serviceCollection.AddInstance<IContextAccessor<AntiForgeryContext>>(contextAccessor); serviceCollection.AddInstance<IScopedInstance<AntiForgeryContext>>(contextAccessor);
return serviceCollection.BuildServiceProvider(); return serviceCollection.BuildServiceProvider();
} }

View File

@ -128,12 +128,12 @@ namespace Microsoft.AspNet.Mvc
return binder; return binder;
} }
private static IContextAccessor<ActionContext> CreateActionContext(HttpContext context) private static IScopedInstance<ActionContext> CreateActionContext(HttpContext context)
{ {
return CreateActionContext(context, (new Mock<IRouter>()).Object); return CreateActionContext(context, (new Mock<IRouter>()).Object);
} }
private static IContextAccessor<ActionContext> CreateActionContext(HttpContext context, IRouter router) private static IScopedInstance<ActionContext> CreateActionContext(HttpContext context, IRouter router)
{ {
var routeData = new RouteData(); var routeData = new RouteData();
routeData.Routers.Add(router); routeData.Routers.Add(router);
@ -141,7 +141,7 @@ namespace Microsoft.AspNet.Mvc
var actionContext = new ActionContext(context, var actionContext = new ActionContext(context,
routeData, routeData,
new ActionDescriptor()); new ActionDescriptor());
var contextAccessor = new Mock<IContextAccessor<ActionContext>>(); var contextAccessor = new Mock<IScopedInstance<ActionContext>>();
contextAccessor.SetupGet(c => c.Value) contextAccessor.SetupGet(c => c.Value)
.Returns(actionContext); .Returns(actionContext);
return contextAccessor.Object; return contextAccessor.Object;

View File

@ -246,12 +246,7 @@ namespace Microsoft.AspNet.Mvc
ILoggerFactory loggerFactory = null, ILoggerFactory loggerFactory = null,
IOptions<MvcOptions> optionsAccessor = null) IOptions<MvcOptions> optionsAccessor = null)
{ {
var mockContextAccessor = new Mock<IContextAccessor<ActionContext>>(); var mockContextAccessor = new Mock<IScopedInstance<ActionContext>>();
mockContextAccessor.Setup(c => c.SetContextSource(
It.IsAny<Func<ActionContext>>(),
It.IsAny<Func<ActionContext, ActionContext>>()))
.Returns(NullDisposable.Instance);
if (actionSelector == null) if (actionSelector == null)
{ {
@ -292,7 +287,7 @@ namespace Microsoft.AspNet.Mvc
} }
var httpContext = new Mock<HttpContext>(); var httpContext = new Mock<HttpContext>();
httpContext.Setup(h => h.RequestServices.GetService(typeof(IContextAccessor<ActionContext>))) httpContext.Setup(h => h.RequestServices.GetService(typeof(IScopedInstance<ActionContext>)))
.Returns(mockContextAccessor.Object); .Returns(mockContextAccessor.Object);
httpContext.Setup(h => h.RequestServices.GetService(typeof(IActionSelector))) httpContext.Setup(h => h.RequestServices.GetService(typeof(IActionSelector)))
.Returns(actionSelector); .Returns(actionSelector);

View File

@ -560,12 +560,12 @@ namespace Microsoft.AspNet.Mvc.Core.Test
return context.Object; return context.Object;
} }
private static IContextAccessor<ActionContext> CreateActionContext(HttpContext context) private static IScopedInstance<ActionContext> CreateActionContext(HttpContext context)
{ {
return CreateActionContext(context, (new Mock<IRouter>()).Object); return CreateActionContext(context, (new Mock<IRouter>()).Object);
} }
private static IContextAccessor<ActionContext> CreateActionContext(HttpContext context, IRouter router) private static IScopedInstance<ActionContext> CreateActionContext(HttpContext context, IRouter router)
{ {
var routeData = new RouteData(); var routeData = new RouteData();
routeData.Routers.Add(router); routeData.Routers.Add(router);
@ -573,7 +573,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test
var actionContext = new ActionContext(context, var actionContext = new ActionContext(context,
routeData, routeData,
new ActionDescriptor()); new ActionDescriptor());
var contextAccessor = new Mock<IContextAccessor<ActionContext>>(); var contextAccessor = new Mock<IScopedInstance<ActionContext>>();
contextAccessor.SetupGet(c => c.Value) contextAccessor.SetupGet(c => c.Value)
.Returns(actionContext); .Returns(actionContext);
return contextAccessor.Object; return contextAccessor.Object;
@ -599,7 +599,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test
return new UrlHelper(actionContext, actionSelector.Object); return new UrlHelper(actionContext, actionSelector.Object);
} }
private static UrlHelper CreateUrlHelper(IContextAccessor<ActionContext> contextAccessor) private static UrlHelper CreateUrlHelper(IScopedInstance<ActionContext> contextAccessor)
{ {
var actionSelector = new Mock<IActionSelector>(MockBehavior.Strict); var actionSelector = new Mock<IActionSelector>(MockBehavior.Strict);
return new UrlHelper(contextAccessor, actionSelector.Object); return new UrlHelper(contextAccessor, actionSelector.Object);

View File

@ -2,15 +2,14 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Hosting;
using Microsoft.Framework.DependencyInjection;
namespace RequestServicesWebSite namespace RequestServicesWebSite
{ {
public class RequestIdService public class RequestIdService
{ {
// This service can only be instantiated by a request-scoped container // This service can only be instantiated by a request-scoped container
public RequestIdService(IServiceProvider services, IContextAccessor<HttpContext> contextAccessor) public RequestIdService(IServiceProvider services, IHttpContextAccessor contextAccessor)
{ {
if (contextAccessor.Value.RequestServices != services) if (contextAccessor.Value.RequestServices != services)
{ {

View File

@ -14,7 +14,7 @@ namespace RoutingWebSite
{ {
private readonly ActionContext _actionContext; private readonly ActionContext _actionContext;
public TestResponseGenerator(IContextAccessor<ActionContext> contextAccessor) public TestResponseGenerator(IScopedInstance<ActionContext> contextAccessor)
{ {
_actionContext = contextAccessor.Value; _actionContext = contextAccessor.Value;
if (_actionContext == null) if (_actionContext == null)

View File

@ -19,8 +19,8 @@ namespace UrlHelperWebSite
private readonly IOptions<AppOptions> _appOptions; private readonly IOptions<AppOptions> _appOptions;
private readonly HttpContext _httpContext; private readonly HttpContext _httpContext;
public CustomUrlHelper(IContextAccessor<ActionContext> contextAccessor, IActionSelector actionSelector, public CustomUrlHelper(IScopedInstance<ActionContext> contextAccessor, IActionSelector actionSelector,
IOptions<AppOptions> appOptions) IOptions<AppOptions> appOptions)
: base(contextAccessor, actionSelector) : base(contextAccessor, actionSelector)
{ {
_appOptions = appOptions; _appOptions = appOptions;

View File

@ -14,7 +14,7 @@ namespace VersioningWebSite
{ {
private readonly ActionContext _actionContext; private readonly ActionContext _actionContext;
public TestResponseGenerator(IContextAccessor<ActionContext> contextAccessor) public TestResponseGenerator(IScopedInstance<ActionContext> contextAccessor)
{ {
_actionContext = contextAccessor.Value; _actionContext = contextAccessor.Value;
if (_actionContext == null) if (_actionContext == null)