React to IContextAccessor -> IScopedInstance
This commit is contained in:
parent
f8cb519c2f
commit
157b633758
|
|
@ -24,7 +24,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
public AntiForgeryToken GetCookieToken(HttpContext httpContext)
|
||||
{
|
||||
var contextAccessor =
|
||||
httpContext.RequestServices.GetRequiredService<IContextAccessor<AntiForgeryContext>>();
|
||||
httpContext.RequestServices.GetRequiredService<IScopedInstance<AntiForgeryContext>>();
|
||||
if (contextAccessor.Value != null)
|
||||
{
|
||||
return contextAccessor.Value.CookieToken;
|
||||
|
|
@ -58,9 +58,9 @@ namespace Microsoft.AspNet.Mvc
|
|||
// 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.
|
||||
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.");
|
||||
contextAccessor.SetValue(new AntiForgeryContext() { CookieToken = token });
|
||||
contextAccessor.Value = new AntiForgeryContext() { CookieToken = token };
|
||||
|
||||
var serializedToken = _serializer.Serialize(token);
|
||||
var options = new CookieOptions() { HttpOnly = true };
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
private readonly IBodyModelValidator _bodyModelValidator;
|
||||
private readonly IValidationExcludeFiltersProvider _bodyValidationExcludeFiltersProvider;
|
||||
|
||||
public BodyModelBinder([NotNull] IContextAccessor<ActionContext> context,
|
||||
public BodyModelBinder([NotNull] IScopedInstance<ActionContext> context,
|
||||
[NotNull] IInputFormatterSelector selector,
|
||||
[NotNull] IBodyModelValidator bodyModelValidator,
|
||||
[NotNull] IValidationExcludeFiltersProvider bodyValidationExcludeFiltersProvider)
|
||||
|
|
|
|||
|
|
@ -95,32 +95,25 @@ namespace Microsoft.AspNet.Mvc
|
|||
var optionsAccessor = services.GetRequiredService<IOptions<MvcOptions>>();
|
||||
actionContext.ModelState.MaxAllowedErrors = optionsAccessor.Options.MaxModelValidationErrors;
|
||||
|
||||
var contextAccessor = services.GetRequiredService<IContextAccessor<ActionContext>>();
|
||||
using (contextAccessor.SetContextSource(() => actionContext, PreventExchange))
|
||||
var contextAccessor = services.GetRequiredService<IScopedInstance<ActionContext>>();
|
||||
contextAccessor.Value = actionContext;
|
||||
var invokerFactory = services.GetRequiredService<IActionInvokerFactory>();
|
||||
var invoker = invokerFactory.CreateInvoker(actionContext);
|
||||
if (invoker == null)
|
||||
{
|
||||
var invokerFactory = services.GetRequiredService<IActionInvokerFactory>();
|
||||
var invoker = invokerFactory.CreateInvoker(actionContext);
|
||||
if (invoker == null)
|
||||
{
|
||||
LogActionSelection(actionSelected: true, actionInvoked: false, handled: context.IsHandled);
|
||||
LogActionSelection(actionSelected: true, actionInvoked: false, handled: context.IsHandled);
|
||||
|
||||
var ex = new InvalidOperationException(
|
||||
Resources.FormatActionInvokerFactory_CouldNotCreateInvoker(
|
||||
actionDescriptor.DisplayName));
|
||||
var ex = new InvalidOperationException(
|
||||
Resources.FormatActionInvokerFactory_CouldNotCreateInvoker(
|
||||
actionDescriptor.DisplayName));
|
||||
|
||||
// Add tracing/logging (what do we think of this pattern of
|
||||
// tacking on extra data on the exception?)
|
||||
ex.Data.Add("AD", actionDescriptor);
|
||||
throw ex;
|
||||
}
|
||||
|
||||
await invoker.InvokeAsync();
|
||||
// Add tracing/logging (what do we think of this pattern of
|
||||
// tacking on extra data on the exception?)
|
||||
ex.Data.Add("AD", actionDescriptor);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
private ActionContext PreventExchange(ActionContext contex)
|
||||
{
|
||||
throw new InvalidOperationException(Resources.ActionContextAccessor_SetValueNotSupported);
|
||||
await invoker.InvokeAsync();
|
||||
}
|
||||
|
||||
private void EnsureLogger(HttpContext context)
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
/// <param name="actionSelector">The <see cref="IActionSelector"/> to be used for verifying the correctness of
|
||||
/// supplied parameters for a route.
|
||||
/// </param>
|
||||
public UrlHelper(IContextAccessor<ActionContext> contextAccessor, IActionSelector actionSelector)
|
||||
public UrlHelper(IScopedInstance<ActionContext> contextAccessor, IActionSelector actionSelector)
|
||||
{
|
||||
_httpContext = contextAccessor.Value.HttpContext;
|
||||
_router = contextAccessor.Value.RouteData.Routers[0];
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ namespace Microsoft.Framework.DependencyInjection
|
|||
services.AddOptions(configuration);
|
||||
services.AddDataProtection(configuration);
|
||||
services.AddRouting(configuration);
|
||||
services.AddContextAccessor(configuration);
|
||||
services.AddScopedInstance(configuration);
|
||||
services.Configure<RouteOptions>(routeOptions =>
|
||||
routeOptions.ConstraintMap
|
||||
.Add("exists",
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test
|
|||
{
|
||||
var httpContext = new Mock<HttpContext>();
|
||||
var actionContext = GetActionContext(httpContext.Object);
|
||||
var mockContentAccessor = new Mock<IContextAccessor<ActionContext>>();
|
||||
var mockContentAccessor = new Mock<IScopedInstance<ActionContext>>();
|
||||
mockContentAccessor.SetupGet(o => o.Value).Returns(actionContext);
|
||||
var mockActionSelector = new Mock<IActionSelector>();
|
||||
var urlHelper = new UrlHelper(mockContentAccessor.Object, mockActionSelector.Object);
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test
|
|||
mockHttpContext
|
||||
.Setup(o => o.Request.Cookies)
|
||||
.Returns(requestCookies.Object);
|
||||
var contextAccessor = new ContextAccessor<AntiForgeryContext>();
|
||||
var contextAccessor = new ScopedInstance<AntiForgeryContext>();
|
||||
mockHttpContext.SetupGet(o => o.RequestServices)
|
||||
.Returns(GetServiceProvider(contextAccessor));
|
||||
var config = new AntiForgeryOptions()
|
||||
|
|
@ -60,13 +60,13 @@ namespace Microsoft.AspNet.Mvc.Core.Test
|
|||
mockHttpContext
|
||||
.Setup(o => o.Request.Cookies)
|
||||
.Returns(requestCookies.Object);
|
||||
var contextAccessor = new ContextAccessor<AntiForgeryContext>();
|
||||
var contextAccessor = new ScopedInstance<AntiForgeryContext>();
|
||||
mockHttpContext.SetupGet(o => o.RequestServices)
|
||||
.Returns(GetServiceProvider(contextAccessor));
|
||||
|
||||
// add a cookie explicitly.
|
||||
var cookie = new AntiForgeryToken();
|
||||
contextAccessor.SetValue(new AntiForgeryContext() { CookieToken = cookie });
|
||||
contextAccessor.Value = new AntiForgeryContext() { CookieToken = cookie };
|
||||
var config = new AntiForgeryOptions()
|
||||
{
|
||||
CookieName = _cookieName
|
||||
|
|
@ -275,7 +275,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test
|
|||
var mockHttpContext = new Mock<HttpContext>();
|
||||
mockHttpContext.Setup(o => o.Response.Cookies)
|
||||
.Returns(cookies);
|
||||
var contextAccessor = new ContextAccessor<AntiForgeryContext>();
|
||||
var contextAccessor = new ScopedInstance<AntiForgeryContext>();
|
||||
mockHttpContext.SetupGet(o => o.RequestServices)
|
||||
.Returns(GetServiceProvider(contextAccessor));
|
||||
|
||||
|
|
@ -317,17 +317,17 @@ namespace Microsoft.AspNet.Mvc.Core.Test
|
|||
mockHttpContext.Setup(o => o.Request)
|
||||
.Returns(request.Object);
|
||||
|
||||
var contextAccessor = new ContextAccessor<AntiForgeryContext>();
|
||||
var contextAccessor = new ScopedInstance<AntiForgeryContext>();
|
||||
mockHttpContext.SetupGet(o => o.RequestServices)
|
||||
.Returns(GetServiceProvider(contextAccessor));
|
||||
|
||||
return mockHttpContext.Object;
|
||||
}
|
||||
|
||||
private static IServiceProvider GetServiceProvider(IContextAccessor<AntiForgeryContext> contextAccessor)
|
||||
private static IServiceProvider GetServiceProvider(IScopedInstance<AntiForgeryContext> contextAccessor)
|
||||
{
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection.AddInstance<IContextAccessor<AntiForgeryContext>>(contextAccessor);
|
||||
serviceCollection.AddInstance<IScopedInstance<AntiForgeryContext>>(contextAccessor);
|
||||
return serviceCollection.BuildServiceProvider();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -128,12 +128,12 @@ namespace Microsoft.AspNet.Mvc
|
|||
return binder;
|
||||
}
|
||||
|
||||
private static IContextAccessor<ActionContext> CreateActionContext(HttpContext context)
|
||||
private static IScopedInstance<ActionContext> CreateActionContext(HttpContext context)
|
||||
{
|
||||
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();
|
||||
routeData.Routers.Add(router);
|
||||
|
|
@ -141,7 +141,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
var actionContext = new ActionContext(context,
|
||||
routeData,
|
||||
new ActionDescriptor());
|
||||
var contextAccessor = new Mock<IContextAccessor<ActionContext>>();
|
||||
var contextAccessor = new Mock<IScopedInstance<ActionContext>>();
|
||||
contextAccessor.SetupGet(c => c.Value)
|
||||
.Returns(actionContext);
|
||||
return contextAccessor.Object;
|
||||
|
|
|
|||
|
|
@ -246,12 +246,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
ILoggerFactory loggerFactory = null,
|
||||
IOptions<MvcOptions> optionsAccessor = null)
|
||||
{
|
||||
var mockContextAccessor = new Mock<IContextAccessor<ActionContext>>();
|
||||
mockContextAccessor.Setup(c => c.SetContextSource(
|
||||
It.IsAny<Func<ActionContext>>(),
|
||||
It.IsAny<Func<ActionContext, ActionContext>>()))
|
||||
.Returns(NullDisposable.Instance);
|
||||
|
||||
var mockContextAccessor = new Mock<IScopedInstance<ActionContext>>();
|
||||
|
||||
if (actionSelector == null)
|
||||
{
|
||||
|
|
@ -292,7 +287,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
}
|
||||
|
||||
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);
|
||||
httpContext.Setup(h => h.RequestServices.GetService(typeof(IActionSelector)))
|
||||
.Returns(actionSelector);
|
||||
|
|
|
|||
|
|
@ -560,12 +560,12 @@ namespace Microsoft.AspNet.Mvc.Core.Test
|
|||
return context.Object;
|
||||
}
|
||||
|
||||
private static IContextAccessor<ActionContext> CreateActionContext(HttpContext context)
|
||||
private static IScopedInstance<ActionContext> CreateActionContext(HttpContext context)
|
||||
{
|
||||
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();
|
||||
routeData.Routers.Add(router);
|
||||
|
|
@ -573,7 +573,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test
|
|||
var actionContext = new ActionContext(context,
|
||||
routeData,
|
||||
new ActionDescriptor());
|
||||
var contextAccessor = new Mock<IContextAccessor<ActionContext>>();
|
||||
var contextAccessor = new Mock<IScopedInstance<ActionContext>>();
|
||||
contextAccessor.SetupGet(c => c.Value)
|
||||
.Returns(actionContext);
|
||||
return contextAccessor.Object;
|
||||
|
|
@ -599,7 +599,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test
|
|||
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);
|
||||
return new UrlHelper(contextAccessor, actionSelector.Object);
|
||||
|
|
|
|||
|
|
@ -2,15 +2,14 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.AspNet.Hosting;
|
||||
|
||||
namespace RequestServicesWebSite
|
||||
{
|
||||
public class RequestIdService
|
||||
{
|
||||
// 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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ namespace RoutingWebSite
|
|||
{
|
||||
private readonly ActionContext _actionContext;
|
||||
|
||||
public TestResponseGenerator(IContextAccessor<ActionContext> contextAccessor)
|
||||
public TestResponseGenerator(IScopedInstance<ActionContext> contextAccessor)
|
||||
{
|
||||
_actionContext = contextAccessor.Value;
|
||||
if (_actionContext == null)
|
||||
|
|
|
|||
|
|
@ -19,8 +19,8 @@ namespace UrlHelperWebSite
|
|||
private readonly IOptions<AppOptions> _appOptions;
|
||||
private readonly HttpContext _httpContext;
|
||||
|
||||
public CustomUrlHelper(IContextAccessor<ActionContext> contextAccessor, IActionSelector actionSelector,
|
||||
IOptions<AppOptions> appOptions)
|
||||
public CustomUrlHelper(IScopedInstance<ActionContext> contextAccessor, IActionSelector actionSelector,
|
||||
IOptions<AppOptions> appOptions)
|
||||
: base(contextAccessor, actionSelector)
|
||||
{
|
||||
_appOptions = appOptions;
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ namespace VersioningWebSite
|
|||
{
|
||||
private readonly ActionContext _actionContext;
|
||||
|
||||
public TestResponseGenerator(IContextAccessor<ActionContext> contextAccessor)
|
||||
public TestResponseGenerator(IScopedInstance<ActionContext> contextAccessor)
|
||||
{
|
||||
_actionContext = contextAccessor.Value;
|
||||
if (_actionContext == null)
|
||||
|
|
|
|||
Loading…
Reference in New Issue