From a21ed4bc514f989b6b07e1762dd0542d2e00cb20 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Thu, 16 Oct 2014 12:02:04 -0700 Subject: [PATCH] Replace calls to GetService with GetRequiredService Even though GetService still exists, GetRequiredService preserves the old behavior of throwing for missing services. --- samples/MvcSample.Web/OverloadController.cs | 2 +- .../ActionResults/JsonResult.cs | 4 ++-- .../ActionResults/ObjectResult.cs | 2 +- .../ActionResults/RedirectResult.cs | 2 +- .../ActionResults/ViewResultBase.cs | 2 +- .../AntiForgery/AntiForgeryTokenStore.cs | 4 ++-- src/Microsoft.AspNet.Mvc.Core/Controller.cs | 2 +- .../DefaultActionDescriptorsCollectionProvider.cs | 2 +- .../DefaultControllerActivator.cs | 2 +- .../Filters/AuthorizeAttribute.cs | 2 +- .../Filters/TypeFilterAttribute.cs | 2 +- .../Filters/ValidateAntiForgeryTokenAttribute.cs | 2 +- src/Microsoft.AspNet.Mvc.Core/Injector.cs | 2 +- .../KnownRouteValueConstraint.cs | 2 +- src/Microsoft.AspNet.Mvc.Core/MvcRouteHandler.cs | 12 ++++++------ .../Rendering/Html/DefaultDisplayTemplates.cs | 6 +++--- .../Rendering/Html/DefaultEditorTemplates.cs | 6 +++--- .../Rendering/Html/TemplateRenderer.cs | 2 +- .../Routing/AttributeRouting.cs | 6 +++--- .../ViewComponents/DefaultViewComponentInvoker.cs | 2 +- .../Razor/PreCompileViews/RazorPreCompiler.cs | 6 +++--- src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs | 4 ++-- src/Microsoft.AspNet.Mvc.Razor/RazorPageOfT.cs | 2 +- src/Microsoft.AspNet.Mvc.Razor/RazorViewEngine.cs | 2 +- .../VirtualPathRazorPageFactory.cs | 2 +- .../ApiController.cs | 6 +++--- .../HttpRequestMessageExtensions.cs | 6 +++--- src/Microsoft.AspNet.Mvc/MvcServices.cs | 2 +- src/Microsoft.AspNet.Mvc/RazorPreCompileModule.cs | 2 +- .../DefaultControllerActivatorTest.cs | 6 ++++++ .../InlineConstraintTests.cs | 4 ++-- .../TestHelper.cs | 2 +- .../FallbackOnTypeBasedMatchController.cs | 2 +- test/WebSites/InlineConstraintsWebSite/Startup.cs | 4 ++-- .../BuilderExtensions.cs | 2 +- test/WebSites/RazorInstrumentationWebsite/Startup.cs | 2 +- .../WebSites/RoutingWebSite/TestResponseGenerator.cs | 2 +- .../VersioningWebSite/TestResponseGenerator.cs | 2 +- 38 files changed, 65 insertions(+), 59 deletions(-) diff --git a/samples/MvcSample.Web/OverloadController.cs b/samples/MvcSample.Web/OverloadController.cs index 36be794e7f..f2bf43b2e9 100644 --- a/samples/MvcSample.Web/OverloadController.cs +++ b/samples/MvcSample.Web/OverloadController.cs @@ -74,7 +74,7 @@ public bool Accept(ActionConstraintContext context) }); var valueProviderFactory = context.RouteContext.HttpContext.RequestServices - .GetService(); + .GetRequiredService(); var factoryContext = new ValueProviderFactoryContext( context.RouteContext.HttpContext, diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/JsonResult.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/JsonResult.cs index 1529af0b85..4ed6f1e06d 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ActionResults/JsonResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/JsonResult.cs @@ -94,7 +94,7 @@ private IOutputFormatter SelectFormatter(OutputFormatterContext formatterContext var defaultFormatters = formatterContext.ActionContext .HttpContext .RequestServices - .GetService() + .GetRequiredService() .OutputFormatters; var formatter = _objectResult.SelectFormatter(formatterContext, defaultFormatters); @@ -103,7 +103,7 @@ private IOutputFormatter SelectFormatter(OutputFormatterContext formatterContext formatter = _defaultFormatter ?? formatterContext.ActionContext .HttpContext .RequestServices - .GetService(); + .GetRequiredService(); } return formatter; diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ObjectResult.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ObjectResult.cs index a6a47e9d81..38f49dd49a 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ObjectResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ObjectResult.cs @@ -209,7 +209,7 @@ private IEnumerable GetDefaultFormatters(ActionContext context { formatters = context.HttpContext .RequestServices - .GetService() + .GetRequiredService() .OutputFormatters; } else diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/RedirectResult.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/RedirectResult.cs index a9c9d86b1d..b957da1af8 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ActionResults/RedirectResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/RedirectResult.cs @@ -34,7 +34,7 @@ public override void ExecuteResult([NotNull] ActionContext context) var destinationUrl = Url; var urlHelper = context.HttpContext .RequestServices - .GetService(); + .GetRequiredService(); // IsLocalUrl is called to handle Urls starting with '~/'. if (urlHelper.IsLocalUrl(destinationUrl)) diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResultBase.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResultBase.cs index 6aa04ead36..fa3fad5152 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResultBase.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResultBase.cs @@ -25,7 +25,7 @@ public abstract class ViewResultBase : ActionResult public override async Task ExecuteResultAsync([NotNull] ActionContext context) { - var viewEngine = ViewEngine ?? context.HttpContext.RequestServices.GetService(); + var viewEngine = ViewEngine ?? context.HttpContext.RequestServices.GetRequiredService(); var viewName = ViewName ?? context.ActionDescriptor.Name; var view = FindViewInternal(viewEngine, context, viewName); diff --git a/src/Microsoft.AspNet.Mvc.Core/AntiForgery/AntiForgeryTokenStore.cs b/src/Microsoft.AspNet.Mvc.Core/AntiForgery/AntiForgeryTokenStore.cs index 4cd1051b81..224d99544c 100644 --- a/src/Microsoft.AspNet.Mvc.Core/AntiForgery/AntiForgeryTokenStore.cs +++ b/src/Microsoft.AspNet.Mvc.Core/AntiForgery/AntiForgeryTokenStore.cs @@ -24,7 +24,7 @@ internal AntiForgeryTokenStore([NotNull] AntiForgeryOptions config, public AntiForgeryToken GetCookieToken(HttpContext httpContext) { - var contextAccessor = httpContext.RequestServices.GetService>(); + var contextAccessor = httpContext.RequestServices.GetRequiredService>(); if (contextAccessor.Value != null) { return contextAccessor.Value.CookieToken; @@ -57,7 +57,7 @@ public void SaveCookieToken(HttpContext httpContext, AntiForgeryToken token) { // 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.GetService>(); + var contextAccessor = httpContext.RequestServices.GetRequiredService>(); Contract.Assert(contextAccessor.Value == null, "AntiForgeryContext should be set only once per request."); contextAccessor.SetValue(new AntiForgeryContext() { CookieToken = token }); diff --git a/src/Microsoft.AspNet.Mvc.Core/Controller.cs b/src/Microsoft.AspNet.Mvc.Core/Controller.cs index ca9e280a1c..1726c54563 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Controller.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Controller.cs @@ -68,7 +68,7 @@ public IViewEngine ViewEngine { _viewEngine = ActionContext?. HttpContext?. - RequestServices.GetService(); + RequestServices.GetRequiredService(); } return _viewEngine; diff --git a/src/Microsoft.AspNet.Mvc.Core/DefaultActionDescriptorsCollectionProvider.cs b/src/Microsoft.AspNet.Mvc.Core/DefaultActionDescriptorsCollectionProvider.cs index bf757acafb..54de9f7b12 100644 --- a/src/Microsoft.AspNet.Mvc.Core/DefaultActionDescriptorsCollectionProvider.cs +++ b/src/Microsoft.AspNet.Mvc.Core/DefaultActionDescriptorsCollectionProvider.cs @@ -43,7 +43,7 @@ public ActionDescriptorsCollection ActionDescriptors private ActionDescriptorsCollection GetCollection() { var actionDescriptorProvider = - _serviceProvider.GetService>(); + _serviceProvider.GetRequiredService>(); var actionDescriptorProviderContext = new ActionDescriptorProviderContext(); actionDescriptorProvider.Invoke(actionDescriptorProviderContext); diff --git a/src/Microsoft.AspNet.Mvc.Core/DefaultControllerActivator.cs b/src/Microsoft.AspNet.Mvc.Core/DefaultControllerActivator.cs index 391b98ef64..eb39d84e01 100644 --- a/src/Microsoft.AspNet.Mvc.Core/DefaultControllerActivator.cs +++ b/src/Microsoft.AspNet.Mvc.Core/DefaultControllerActivator.cs @@ -72,7 +72,7 @@ protected virtual IReadOnlyDictionary> CreateV { var serviceProvider = context.HttpContext.RequestServices; return new ViewDataDictionary( - serviceProvider.GetService(), + serviceProvider.GetRequiredService(), context.ModelState); } } diff --git a/src/Microsoft.AspNet.Mvc.Core/Filters/AuthorizeAttribute.cs b/src/Microsoft.AspNet.Mvc.Core/Filters/AuthorizeAttribute.cs index e5be03c7da..836b447921 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Filters/AuthorizeAttribute.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Filters/AuthorizeAttribute.cs @@ -60,7 +60,7 @@ public override async Task OnAuthorizationAsync([NotNull] AuthorizationContext c } else { - var authorizationService = httpContext.RequestServices.GetService(); + var authorizationService = httpContext.RequestServices.GetRequiredService(); if (authorizationService == null) { diff --git a/src/Microsoft.AspNet.Mvc.Core/Filters/TypeFilterAttribute.cs b/src/Microsoft.AspNet.Mvc.Core/Filters/TypeFilterAttribute.cs index 6c292eb6c5..d932a4a033 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Filters/TypeFilterAttribute.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Filters/TypeFilterAttribute.cs @@ -25,7 +25,7 @@ public TypeFilterAttribute([NotNull] Type type) public IFilter CreateInstance([NotNull] IServiceProvider serviceProvider) { - var activator = serviceProvider.GetService(); + var activator = serviceProvider.GetRequiredService(); var obj = activator.CreateInstance(serviceProvider, ImplementationType, Arguments ?? new object[0]); var filter = obj as IFilter; diff --git a/src/Microsoft.AspNet.Mvc.Core/Filters/ValidateAntiForgeryTokenAttribute.cs b/src/Microsoft.AspNet.Mvc.Core/Filters/ValidateAntiForgeryTokenAttribute.cs index 0275330792..cb9247845b 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Filters/ValidateAntiForgeryTokenAttribute.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Filters/ValidateAntiForgeryTokenAttribute.cs @@ -13,7 +13,7 @@ public class ValidateAntiForgeryTokenAttribute : Attribute, IFilterFactory, IOrd public IFilter CreateInstance(IServiceProvider serviceProvider) { - var antiForgery = serviceProvider.GetService(); + var antiForgery = serviceProvider.GetRequiredService(); return new ValidateAntiForgeryTokenAuthorizationFilter(antiForgery); } } diff --git a/src/Microsoft.AspNet.Mvc.Core/Injector.cs b/src/Microsoft.AspNet.Mvc.Core/Injector.cs index 3860da7bb3..7a57907074 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Injector.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Injector.cs @@ -65,7 +65,7 @@ public static void InjectProperty( if (property.PropertyType.IsAssignableFrom(typeof(TProperty))) { - property.SetValue(obj, services.GetService()); + property.SetValue(obj, services.GetRequiredService()); } } } diff --git a/src/Microsoft.AspNet.Mvc.Core/KnownRouteValueConstraint.cs b/src/Microsoft.AspNet.Mvc.Core/KnownRouteValueConstraint.cs index 20a3c443c6..4e9bfd8d7e 100644 --- a/src/Microsoft.AspNet.Mvc.Core/KnownRouteValueConstraint.cs +++ b/src/Microsoft.AspNet.Mvc.Core/KnownRouteValueConstraint.cs @@ -71,7 +71,7 @@ private string[] GetAndCacheAllMatchingValues(string routeKey, HttpContext httpC private static ActionDescriptorsCollection GetAndValidateActionDescriptorsCollection(HttpContext httpContext) { var provider = httpContext.RequestServices - .GetService(); + .GetRequiredService(); var descriptors = provider.ActionDescriptors; if (descriptors == null) diff --git a/src/Microsoft.AspNet.Mvc.Core/MvcRouteHandler.cs b/src/Microsoft.AspNet.Mvc.Core/MvcRouteHandler.cs index d3e477c427..c4c08881f2 100644 --- a/src/Microsoft.AspNet.Mvc.Core/MvcRouteHandler.cs +++ b/src/Microsoft.AspNet.Mvc.Core/MvcRouteHandler.cs @@ -23,7 +23,7 @@ public string GetVirtualPath([NotNull] VirtualPathContext context) { // The contract of this method is to check that the values coming in from the route are valid; // that they match an existing action, setting IsBound = true if the values are OK. - var actionSelector = context.Context.RequestServices.GetService(); + var actionSelector = context.Context.RequestServices.GetRequiredService(); context.IsBound = actionSelector.HasValidAction(context); // We return null here because we're not responsible for generating the url, the route is. @@ -47,7 +47,7 @@ public async Task RouteAsync([NotNull] RouteContext context) EnsureLogger(context.HttpContext); using (_logger.BeginScope("MvcRouteHandler.RouteAsync")) { - var actionSelector = services.GetService(); + var actionSelector = services.GetRequiredService(); var actionDescriptor = await actionSelector.SelectAsync(context); if (actionDescriptor == null) @@ -78,13 +78,13 @@ public async Task RouteAsync([NotNull] RouteContext context) var actionContext = new ActionContext(context.HttpContext, context.RouteData, actionDescriptor); - var optionsAccessor = services.GetService>(); + var optionsAccessor = services.GetRequiredService>(); actionContext.ModelState.MaxAllowedErrors = optionsAccessor.Options.MaxModelValidationErrors; - var contextAccessor = services.GetService>(); + var contextAccessor = services.GetRequiredService>(); using (contextAccessor.SetContextSource(() => actionContext, PreventExchange)) { - var invokerFactory = services.GetService(); + var invokerFactory = services.GetRequiredService(); var invoker = invokerFactory.CreateInvoker(actionContext); if (invoker == null) { @@ -134,7 +134,7 @@ private void EnsureLogger(HttpContext context) { if (_logger == null) { - var factory = context.RequestServices.GetService(); + var factory = context.RequestServices.GetRequiredService(); _logger = factory.Create(); } } diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/DefaultDisplayTemplates.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/DefaultDisplayTemplates.cs index 548c8c0ea5..5cfba3228b 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/DefaultDisplayTemplates.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/DefaultDisplayTemplates.cs @@ -124,8 +124,8 @@ public static string CollectionTemplate(IHtmlHelper html) var result = new StringBuilder(); var serviceProvider = html.ViewContext.HttpContext.RequestServices; - var metadataProvider = serviceProvider.GetService(); - var viewEngine = serviceProvider.GetService(); + var metadataProvider = serviceProvider.GetRequiredService(); + var viewEngine = serviceProvider.GetRequiredService(); var index = 0; foreach (var item in collection) @@ -217,7 +217,7 @@ public static string ObjectTemplate(IHtmlHelper html) } var serviceProvider = html.ViewContext.HttpContext.RequestServices; - var viewEngine = serviceProvider.GetService(); + var viewEngine = serviceProvider.GetRequiredService(); foreach (var propertyMetadata in modelMetadata.Properties.Where(pm => ShouldShow(pm, templateInfo))) { diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/DefaultEditorTemplates.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/DefaultEditorTemplates.cs index 5cccbc1232..f54c2c7a70 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/DefaultEditorTemplates.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/DefaultEditorTemplates.cs @@ -84,8 +84,8 @@ public static string CollectionTemplate(IHtmlHelper html) var result = new StringBuilder(); var serviceProvider = html.ViewContext.HttpContext.RequestServices; - var metadataProvider = serviceProvider.GetService(); - var viewEngine = serviceProvider.GetService(); + var metadataProvider = serviceProvider.GetRequiredService(); + var viewEngine = serviceProvider.GetRequiredService(); var index = 0; foreach (var item in collection) @@ -225,7 +225,7 @@ public static string ObjectTemplate(IHtmlHelper html) } var serviceProvider = html.ViewContext.HttpContext.RequestServices; - var viewEngine = serviceProvider.GetService(); + var viewEngine = serviceProvider.GetRequiredService(); foreach (var propertyMetadata in modelMetadata.Properties.Where(pm => ShouldShow(pm, templateInfo))) { diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/TemplateRenderer.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/TemplateRenderer.cs index eb14e9ccf8..6698000d1a 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/TemplateRenderer.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/TemplateRenderer.cs @@ -197,7 +197,7 @@ private IEnumerable GetViewNames() private static IHtmlHelper MakeHtmlHelper(ViewContext viewContext, ViewDataDictionary viewData) { - var newHelper = viewContext.HttpContext.RequestServices.GetService(); + var newHelper = viewContext.HttpContext.RequestServices.GetRequiredService(); var contextable = newHelper as ICanHasViewContext; if (contextable != null) diff --git a/src/Microsoft.AspNet.Mvc.Core/Routing/AttributeRouting.cs b/src/Microsoft.AspNet.Mvc.Core/Routing/AttributeRouting.cs index f85f46e05a..0d416fbeea 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Routing/AttributeRouting.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Routing/AttributeRouting.cs @@ -28,7 +28,7 @@ public static IRouter CreateAttributeMegaRoute([NotNull] IRouter target, [NotNul { var actions = GetActionDescriptors(services); - var inlineConstraintResolver = services.GetService(); + var inlineConstraintResolver = services.GetRequiredService(); var routeInfos = GetRouteInfos(inlineConstraintResolver, actions); // We're creating one AttributeRouteGenerationEntry per action. This allows us to match the intended @@ -79,12 +79,12 @@ public static IRouter CreateAttributeMegaRoute([NotNull] IRouter target, [NotNul target, matchingEntries, generationEntries, - services.GetService()); + services.GetRequiredService()); } private static IReadOnlyList GetActionDescriptors(IServiceProvider services) { - var actionDescriptorProvider = services.GetService(); + var actionDescriptorProvider = services.GetRequiredService(); var actionDescriptorsCollection = actionDescriptorProvider.ActionDescriptors; return actionDescriptorsCollection.Items; diff --git a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/DefaultViewComponentInvoker.cs b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/DefaultViewComponentInvoker.cs index cc68225114..b5e4d974dc 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/DefaultViewComponentInvoker.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/DefaultViewComponentInvoker.cs @@ -74,7 +74,7 @@ public async Task InvokeAsync([NotNull] ViewComponentContext context) private object CreateComponent([NotNull] ViewContext context) { - var activator = _serviceProvider.GetService(); + var activator = _serviceProvider.GetRequiredService(); var component = activator.CreateInstance(_serviceProvider, _componentType.AsType()); _viewComponentActivator.Activate(component, context); return component; diff --git a/src/Microsoft.AspNet.Mvc.Razor/Razor/PreCompileViews/RazorPreCompiler.cs b/src/Microsoft.AspNet.Mvc.Razor/Razor/PreCompileViews/RazorPreCompiler.cs index 108beeeb7c..2c72199bc4 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Razor/PreCompileViews/RazorPreCompiler.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Razor/PreCompileViews/RazorPreCompiler.cs @@ -21,8 +21,8 @@ public class RazorPreCompiler public RazorPreCompiler([NotNull] IServiceProvider designTimeServiceProvider) : this(designTimeServiceProvider, - designTimeServiceProvider.GetService(), - designTimeServiceProvider.GetService>()) + designTimeServiceProvider.GetRequiredService(), + designTimeServiceProvider.GetRequiredService>()) { } @@ -34,7 +34,7 @@ public RazorPreCompiler([NotNull] IServiceProvider designTimeServiceProvider, _host = host; _host.EnableInstrumentation = true; - var appEnv = _serviceProvider.GetService(); + var appEnv = _serviceProvider.GetRequiredService(); _fileSystem = optionsAccessor.Options.FileSystem; } diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs index bc46922d89..6bf5fd5a88 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs @@ -119,7 +119,7 @@ private ITagHelperActivator TagHelperActivator { if (_tagHelperActivator == null) { - _tagHelperActivator = ViewContext.HttpContext.RequestServices.GetService(); + _tagHelperActivator = ViewContext.HttpContext.RequestServices.GetRequiredService(); } return _tagHelperActivator; @@ -393,7 +393,7 @@ public virtual string Href([NotNull] string contentPath) { if (_urlHelper == null) { - _urlHelper = Context.RequestServices.GetService(); + _urlHelper = Context.RequestServices.GetRequiredService(); } return _urlHelper.Content(contentPath); diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorPageOfT.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorPageOfT.cs index c5facfed86..4996850e27 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorPageOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorPageOfT.cs @@ -43,7 +43,7 @@ public ModelExpression CreateModelExpression([NotNull] Expression(); + _provider = Context.RequestServices.GetRequiredService(); } var name = ExpressionHelper.GetExpressionText(expression); diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorViewEngine.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorViewEngine.cs index a6a5f2ab10..c1864fa5c0 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorViewEngine.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorViewEngine.cs @@ -183,7 +183,7 @@ private ViewEngineResult CreateFoundResult(ActionContext actionContext, // and might store state. We'll use the service container to create new instances as we require. var services = actionContext.HttpContext.RequestServices; - var view = services.GetService(); + var view = services.GetRequiredService(); view.Contextualize(page, partial); return ViewEngineResult.Found(viewName, view); diff --git a/src/Microsoft.AspNet.Mvc.Razor/VirtualPathRazorPageFactory.cs b/src/Microsoft.AspNet.Mvc.Razor/VirtualPathRazorPageFactory.cs index eb1addcbd9..a407538ac7 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/VirtualPathRazorPageFactory.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/VirtualPathRazorPageFactory.cs @@ -43,7 +43,7 @@ private IRazorCompilationService RazorCompilationService // it is ok to use the cached service provider because both this, and the // resolved service are in a lifetime of Scoped. // We don't want to get it upgront because it will force Roslyn to load. - _razorcompilationService = _serviceProvider.GetService(); + _razorcompilationService = _serviceProvider.GetRequiredService(); } return _razorcompilationService; diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ApiController.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ApiController.cs index 7a503c84bf..c7b1d22c1f 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ApiController.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ApiController.cs @@ -107,10 +107,10 @@ public void Validate(TEntity entity) /// public void Validate(TEntity entity, string keyPrefix) { - var validator = Context.RequestServices.GetService(); - var metadataProvider = Context.RequestServices.GetService(); + var validator = Context.RequestServices.GetRequiredService(); + var metadataProvider = Context.RequestServices.GetRequiredService(); var modelMetadata = metadataProvider.GetMetadataForType(() => entity, typeof(TEntity)); - var validatorProvider = Context.RequestServices.GetService(); + var validatorProvider = Context.RequestServices.GetRequiredService(); var modelValidationContext = new ModelValidationContext(metadataProvider, validatorProvider, ModelState, diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpRequestMessage/HttpRequestMessageExtensions.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpRequestMessage/HttpRequestMessageExtensions.cs index b4bd72ff6c..bce4ef5c99 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpRequestMessage/HttpRequestMessageExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpRequestMessage/HttpRequestMessageExtensions.cs @@ -214,11 +214,11 @@ public static HttpResponseMessage CreateResponse( if (formatters == null) { // Get the default formatters from options - var options = context.RequestServices.GetService>(); + var options = context.RequestServices.GetRequiredService>(); formatters = options.Options.Formatters; } - var contentNegotiator = context.RequestServices.GetService(); + var contentNegotiator = context.RequestServices.GetRequiredService(); var result = contentNegotiator.Negotiate(typeof(T), request, formatters); if (result?.Formatter == null) @@ -266,7 +266,7 @@ public static HttpResponseMessage CreateResponse( var context = GetHttpContext(request); // Get the default formatters from options - var options = context.RequestServices.GetService>(); + var options = context.RequestServices.GetRequiredService>(); var formatters = options.Options.Formatters; var formatter = formatters.FindWriter(typeof(T), mediaType); diff --git a/src/Microsoft.AspNet.Mvc/MvcServices.cs b/src/Microsoft.AspNet.Mvc/MvcServices.cs index 9c2cb6c596..aebb9ea25a 100644 --- a/src/Microsoft.AspNet.Mvc/MvcServices.cs +++ b/src/Microsoft.AspNet.Mvc/MvcServices.cs @@ -108,7 +108,7 @@ public static IEnumerable GetDefaultServices(IConfiguration // The host is designed to be discarded after consumption and is very inexpensive to initialize. yield return describe.Transient(serviceProvider => { - var optionsAccessor = serviceProvider.GetService>(); + var optionsAccessor = serviceProvider.GetRequiredService>(); return new MvcRazorHost(optionsAccessor.Options.FileSystem); }); diff --git a/src/Microsoft.AspNet.Mvc/RazorPreCompileModule.cs b/src/Microsoft.AspNet.Mvc/RazorPreCompileModule.cs index ce291482ac..3b00f894d8 100644 --- a/src/Microsoft.AspNet.Mvc/RazorPreCompileModule.cs +++ b/src/Microsoft.AspNet.Mvc/RazorPreCompileModule.cs @@ -27,7 +27,7 @@ public RazorPreCompileModule(IServiceProvider services) public virtual void BeforeCompile(IBeforeCompileContext context) { var sc = new ServiceCollection(); - var appEnv = _appServices.GetService(); + var appEnv = _appServices.GetRequiredService(); var setup = new RazorViewEngineOptionsSetup(appEnv); var accessor = new OptionsManager(new[] { setup }); diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/DefaultControllerActivatorTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/DefaultControllerActivatorTest.cs index 0edc0555a5..87fcb9f93b 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/DefaultControllerActivatorTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/DefaultControllerActivatorTest.cs @@ -21,6 +21,8 @@ public void Activate_SetsPropertiesFromActionContextHierarchy() var services = new Mock(); services.Setup(s => s.GetService(typeof(IUrlHelper))) .Returns(Mock.Of()); + services.Setup(s => s.GetService(typeof(IModelMetadataProvider))) + .Returns(Mock.Of()); var httpRequest = Mock.Of(); var httpContext = new Mock(); @@ -83,6 +85,8 @@ public void Activate_PopulatesServicesFromServiceContainer() var services = new Mock(); services.Setup(s => s.GetService(typeof(IUrlHelper))) .Returns(urlHelper); + services.Setup(s => s.GetService(typeof(IModelMetadataProvider))) + .Returns(Mock.Of()); var httpContext = new Mock(); httpContext.SetupGet(c => c.RequestServices) @@ -109,6 +113,8 @@ public void Activate_IgnoresPropertiesThatAreNotDecoratedWithActivateAttribute() var services = new Mock(); services.Setup(s => s.GetService(typeof(IUrlHelper))) .Returns(Mock.Of()); + services.Setup(s => s.GetService(typeof(IModelMetadataProvider))) + .Returns(Mock.Of()); var httpContext = new Mock(); httpContext.SetupGet(c => c.Response) diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/InlineConstraintTests.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/InlineConstraintTests.cs index b59cd87ca0..25c897b115 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/InlineConstraintTests.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/InlineConstraintTests.cs @@ -29,7 +29,7 @@ public InlineConstraintTests() [Fact] public async Task RoutingToANonExistantArea_WithExistConstraint_RoutesToCorrectAction() { - var svc = _provider.GetService(); + var svc = _provider.GetRequiredService(); svc.AddArgument("--TemplateCollection:areaRoute:TemplateValue=" + "{area:exists}/{controller=Home}/{action=Index}"); svc.AddArgument("--TemplateCollection:actionAsMethod:TemplateValue=" + @@ -51,7 +51,7 @@ public async Task RoutingToANonExistantArea_WithExistConstraint_RoutesToCorrectA public async Task RoutingToANonExistantArea_WithoutExistConstraint_RoutesToIncorrectAction() { // Arrange - var svc = _provider.GetService(); + var svc = _provider.GetRequiredService(); svc.AddArgument("--TemplateCollection:areaRoute:TemplateValue=" + "{area}/{controller=Home}/{action=Index}"); svc.AddArgument("--TemplateCollection:actionAsMethod:TemplateValue" + diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs index 1973ecab12..f2aace7942 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs @@ -25,7 +25,7 @@ public static IServiceProvider CreateServices(string applicationWebSiteName) public static IServiceProvider CreateServices(string applicationWebSiteName, string applicationPath) { var originalProvider = CallContextServiceLocator.Locator.ServiceProvider; - var appEnvironment = originalProvider.GetService(); + var appEnvironment = originalProvider.GetRequiredService(); // When an application executes in a regular context, the application base path points to the root // directory where the application is located, for example MvcSample.Web. However, when executing diff --git a/test/WebSites/ConnegWebSite/Controllers/FallbackOnTypeBasedMatchController.cs b/test/WebSites/ConnegWebSite/Controllers/FallbackOnTypeBasedMatchController.cs index 625fd0498f..350d9d199b 100644 --- a/test/WebSites/ConnegWebSite/Controllers/FallbackOnTypeBasedMatchController.cs +++ b/test/WebSites/ConnegWebSite/Controllers/FallbackOnTypeBasedMatchController.cs @@ -56,7 +56,7 @@ public IActionResult OverrideTheFallback_UsingCustomFormatters(int input) public IActionResult OverrideTheFallback_WithDefaultFormatters(int input) { var objectResult = new ObjectResult(input); - var formattersProvider = ActionContext.HttpContext.RequestServices.GetService(); + var formattersProvider = ActionContext.HttpContext.RequestServices.GetRequiredService(); objectResult.Formatters.Add(new HttpNotAcceptableOutputFormatter()); foreach (var formatter in formattersProvider.OutputFormatters) { diff --git a/test/WebSites/InlineConstraintsWebSite/Startup.cs b/test/WebSites/InlineConstraintsWebSite/Startup.cs index d1d8b181b1..05e818f8fc 100644 --- a/test/WebSites/InlineConstraintsWebSite/Startup.cs +++ b/test/WebSites/InlineConstraintsWebSite/Startup.cs @@ -20,7 +20,7 @@ public void Configure(IApplicationBuilder app) configuration.AddEnvironmentVariables(); - var commandLineBuilder = app.ApplicationServices.GetService(); + var commandLineBuilder = app.ApplicationServices.GetRequiredService(); string appConfigPath; if (configuration.TryGet("AppConfigPath", out appConfigPath)) { @@ -33,7 +33,7 @@ public void Configure(IApplicationBuilder app) } else { - var basePath = app.ApplicationServices.GetService().ApplicationBasePath; + var basePath = app.ApplicationServices.GetRequiredService().ApplicationBasePath; configuration.AddJsonFile(Path.Combine(basePath, @"App_Data\config.json")); } diff --git a/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/BuilderExtensions.cs b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/BuilderExtensions.cs index 8813249a29..6029c25b94 100644 --- a/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/BuilderExtensions.cs +++ b/test/WebSites/Microsoft.AspNet.Mvc.TestConfiguration/BuilderExtensions.cs @@ -11,7 +11,7 @@ public static class BuilderExtensions { public static Configuration GetTestConfiguration(this IApplicationBuilder app) { - var configurationProvider = app.ApplicationServices.GetService(); + var configurationProvider = app.ApplicationServices.GetRequiredService(); var configuration = configurationProvider == null ? new Configuration() : configurationProvider.Configuration; diff --git a/test/WebSites/RazorInstrumentationWebsite/Startup.cs b/test/WebSites/RazorInstrumentationWebsite/Startup.cs index 4f8987bd3a..a14ea6cfba 100644 --- a/test/WebSites/RazorInstrumentationWebsite/Startup.cs +++ b/test/WebSites/RazorInstrumentationWebsite/Startup.cs @@ -27,7 +27,7 @@ public void Configure(IApplicationBuilder app) { if (!string.IsNullOrEmpty(context.Request.Headers["ENABLE-RAZOR-INSTRUMENTATION"])) { - var pageExecutionContext = context.ApplicationServices.GetService(); + var pageExecutionContext = context.ApplicationServices.GetRequiredService(); var listenerFeature = new TestPageExecutionListenerFeature(pageExecutionContext); context.SetFeature(listenerFeature); } diff --git a/test/WebSites/RoutingWebSite/TestResponseGenerator.cs b/test/WebSites/RoutingWebSite/TestResponseGenerator.cs index 3995ed4dc0..8b9dda4025 100644 --- a/test/WebSites/RoutingWebSite/TestResponseGenerator.cs +++ b/test/WebSites/RoutingWebSite/TestResponseGenerator.cs @@ -33,7 +33,7 @@ public JsonResult Generate(params string[] expectedUrls) .Where(kvp => kvp.Key != "link" && kvp.Key != "link_action" && kvp.Key != "link_controller") .ToDictionary(kvp => kvp.Key.Substring("link_".Length), kvp => (object)kvp.Value[0]); - var urlHelper = _actionContext.HttpContext.RequestServices.GetService(); + var urlHelper = _actionContext.HttpContext.RequestServices.GetRequiredService(); link = urlHelper.Action(query["link_action"], query["link_controller"], values); } diff --git a/test/WebSites/VersioningWebSite/TestResponseGenerator.cs b/test/WebSites/VersioningWebSite/TestResponseGenerator.cs index 8c032cebe4..79e0ea3c57 100644 --- a/test/WebSites/VersioningWebSite/TestResponseGenerator.cs +++ b/test/WebSites/VersioningWebSite/TestResponseGenerator.cs @@ -33,7 +33,7 @@ public JsonResult Generate(params string[] expectedUrls) .Where(kvp => kvp.Key != "link" && kvp.Key != "link_action" && kvp.Key != "link_controller") .ToDictionary(kvp => kvp.Key.Substring("link_".Length), kvp => (object)kvp.Value[0]); - var urlHelper = _actionContext.HttpContext.RequestServices.GetService(); + var urlHelper = _actionContext.HttpContext.RequestServices.GetRequiredService(); link = urlHelper.Action(query["link_action"], query["link_controller"], values); }