private MvcRouteHandler CreateMvcRouteHandler( ActionDescriptor actionDescriptor = null, IActionSelector actionSelector = null, IActionInvokerFactory invokerFactory = null, ILoggerFactory loggerFactory = null, object diagnosticListener = null) { var actionContextAccessor = new ActionContextAccessor(); if (actionDescriptor == null) { var mockAction = new Mock <ActionDescriptor>(); actionDescriptor = mockAction.Object; } if (actionSelector == null) { var mockActionSelector = new Mock <IActionSelector>(); mockActionSelector .Setup(a => a.SelectCandidates(It.IsAny <RouteContext>())) .Returns(new ActionDescriptor[] { actionDescriptor }); mockActionSelector .Setup(a => a.SelectBestCandidate(It.IsAny <RouteContext>(), It.IsAny <IReadOnlyList <ActionDescriptor> >())) .Returns(actionDescriptor); actionSelector = mockActionSelector.Object; } if (loggerFactory == null) { loggerFactory = NullLoggerFactory.Instance; } var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore"); if (diagnosticListener != null) { diagnosticSource.SubscribeWithAdapter(diagnosticListener); } if (invokerFactory == null) { var mockInvoker = new Mock <IActionInvoker>(); mockInvoker.Setup(i => i.InvokeAsync()) .Returns(Task.FromResult(true)); var mockInvokerFactory = new Mock <IActionInvokerFactory>(); mockInvokerFactory.Setup(f => f.CreateInvoker(It.IsAny <ActionContext>())) .Returns(mockInvoker.Object); invokerFactory = mockInvokerFactory.Object; } return(new MvcRouteHandler( invokerFactory, actionSelector, diagnosticSource, loggerFactory, actionContextAccessor)); }
public static HttpContextAccessor CreateHttpContextAccessor(RequestTelemetry requestTelemetry = null, ActionContext actionContext = null) { var services = new ServiceCollection(); var request = new DefaultHttpContext().Request; request.Method = "GET"; request.Path = new PathString("/Test"); var contextAccessor = new HttpContextAccessor() { HttpContext = request.HttpContext }; services.AddSingleton<IHttpContextAccessor>(contextAccessor); if (actionContext != null) { var si = new ActionContextAccessor(); si.ActionContext = actionContext; services.AddSingleton<IActionContextAccessor>(si); } if (requestTelemetry != null) { services.AddSingleton<RequestTelemetry>(requestTelemetry); } IServiceProvider serviceProvider = services.BuildServiceProvider(); contextAccessor.HttpContext.RequestServices = serviceProvider; return contextAccessor; }
private static void DomainFunc() { var accessor = new ActionContextAccessor(); Assert.Equal(null, accessor.ActionContext); accessor.ActionContext = new ActionContext(); }
private MvcRouteHandler CreateMvcRouteHandler( ActionDescriptor actionDescriptor = null, IActionSelector actionSelector = null, IActionInvokerFactory invokerFactory = null, ILoggerFactory loggerFactory = null, object diagnosticListener = null) { var actionContextAccessor = new ActionContextAccessor(); if (actionDescriptor == null) { var mockAction = new Mock<ActionDescriptor>(); actionDescriptor = mockAction.Object; } if (actionSelector == null) { var mockActionSelector = new Mock<IActionSelector>(); mockActionSelector .Setup(a => a.SelectCandidates(It.IsAny<RouteContext>())) .Returns(new ActionDescriptor[] { actionDescriptor }); mockActionSelector .Setup(a => a.SelectBestCandidate(It.IsAny<RouteContext>(), It.IsAny<IReadOnlyList<ActionDescriptor>>())) .Returns(actionDescriptor); actionSelector = mockActionSelector.Object; } if (loggerFactory == null) { loggerFactory = NullLoggerFactory.Instance; } var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore"); if (diagnosticListener != null) { diagnosticSource.SubscribeWithAdapter(diagnosticListener); } if (invokerFactory == null) { var mockInvoker = new Mock<IActionInvoker>(); mockInvoker.Setup(i => i.InvokeAsync()) .Returns(Task.FromResult(true)); var mockInvokerFactory = new Mock<IActionInvokerFactory>(); mockInvokerFactory.Setup(f => f.CreateInvoker(It.IsAny<ActionContext>())) .Returns(mockInvoker.Object); invokerFactory = mockInvokerFactory.Object; } return new MvcRouteHandler( invokerFactory, actionSelector, diagnosticSource, loggerFactory, actionContextAccessor); }
public void ChangingAppDomainsDoesNotBreak_ActionContextAccessor() { // Arrange var accessor = new ActionContextAccessor(); var context = new ActionContext(); var domain = AppDomain.CreateDomain("newDomain"); // Act domain.DoCallBack(DomainFunc); AppDomain.Unload(domain); accessor.ActionContext = context; // Assert Assert.True(ReferenceEquals(context, accessor.ActionContext)); }