diff --git a/src/Microsoft.AspNet.Mvc.Core/Builder/MvcApplicationBuilderExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/Builder/MvcApplicationBuilderExtensions.cs index 383240b0a8..3f92d61da5 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Builder/MvcApplicationBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Builder/MvcApplicationBuilderExtensions.cs @@ -2,10 +2,12 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using Microsoft.AspNet.Mvc.Core; using Microsoft.AspNet.Mvc.Infrastructure; using Microsoft.AspNet.Mvc.Internal; using Microsoft.AspNet.Mvc.Routing; using Microsoft.AspNet.Routing; +using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNet.Builder { @@ -77,7 +79,13 @@ namespace Microsoft.AspNet.Builder // Verify if AddMvc was done before calling UseMvc // We use the MvcMarkerService to make sure if all the services were added. - MvcServicesHelper.ThrowIfMvcNotRegistered(app.ApplicationServices); + if (app.ApplicationServices.GetService(typeof(MvcMarkerService)) == null) + { + throw new InvalidOperationException(Resources.FormatUnableToFindServices( + nameof(IServiceCollection), + "AddMvc", + "ConfigureServices(...)")); + } var routes = new RouteBuilder(app) { diff --git a/src/Microsoft.AspNet.Mvc.Core/Infrastructure/MvcRouteHandler.cs b/src/Microsoft.AspNet.Mvc.Core/Infrastructure/MvcRouteHandler.cs index da8686b3e2..3839c1866d 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Infrastructure/MvcRouteHandler.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Infrastructure/MvcRouteHandler.cs @@ -119,10 +119,6 @@ namespace Microsoft.AspNet.Mvc.Infrastructure var services = context.RequestServices; - // Verify if AddMvc was done before calling UseMvc - // We use the MvcMarkerService to make sure if all the services were added. - MvcServicesHelper.ThrowIfMvcNotRegistered(services); - // The IActionContextAccessor is optional. We want to avoid the overhead of using CallContext // if possible. _actionContextAccessor = services.GetService(); diff --git a/src/Microsoft.AspNet.Mvc.Core/Internal/MvcMarkerService.cs b/src/Microsoft.AspNet.Mvc.Core/Internal/MvcMarkerService.cs index d7a56267aa..bda9c2219a 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Internal/MvcMarkerService.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Internal/MvcMarkerService.cs @@ -1,11 +1,13 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using Microsoft.Extensions.DependencyInjection; + namespace Microsoft.AspNet.Mvc.Internal { /// - /// This is a Marker class which is used to determine if all the services were added - /// to when Mvc is loaded. + /// A marker class used to determine if all the MVC services were added + /// to the before MVC is configured. /// public class MvcMarkerService { diff --git a/src/Microsoft.AspNet.Mvc.Core/Internal/MvcServicesHelper.cs b/src/Microsoft.AspNet.Mvc.Core/Internal/MvcServicesHelper.cs deleted file mode 100644 index 379f904c8b..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/Internal/MvcServicesHelper.cs +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.Mvc.Core; - -namespace Microsoft.AspNet.Mvc.Internal -{ - /// - /// Helper class which contains MvcServices related helpers. - /// - public static class MvcServicesHelper - { - /// - /// Throws InvalidOperationException when MvcMarkerService is not present - /// in the list of services. - /// - /// The list of services. - public static void ThrowIfMvcNotRegistered(IServiceProvider services) - { - if (services.GetService(typeof(MvcMarkerService)) == null) - { - throw new InvalidOperationException(Resources.FormatUnableToFindServices( - "IServiceCollection.AddMvc()", - "IApplicationBuilder.ConfigureServices(...)", - "IApplicationBuilder.UseMvc(...)")); - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/Properties/Resources.Designer.cs b/src/Microsoft.AspNet.Mvc.Core/Properties/Resources.Designer.cs index 671585b669..712a56ea3b 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Properties/Resources.Designer.cs @@ -475,7 +475,7 @@ namespace Microsoft.AspNet.Mvc.Core } /// - /// Unable to find the required services. Please add all the required services by calling '{0}' inside the call to '{1}' or '{2}' in the application startup code. + /// Unable to find the required services. Please add all the required services by calling '{0}.{1}' inside the call to '{4}' in the application startup code. /// internal static string UnableToFindServices { @@ -483,11 +483,11 @@ namespace Microsoft.AspNet.Mvc.Core } /// - /// Unable to find the required services. Please add all the required services by calling '{0}' inside the call to '{1}' or '{2}' in the application startup code. + /// Unable to find the required services. Please add all the required services by calling '{0}.{1}' inside the call to '{4}' in the application startup code. /// - internal static string FormatUnableToFindServices(object p0, object p1, object p2) + internal static string FormatUnableToFindServices(object p0, object p1, object p4) { - return string.Format(CultureInfo.CurrentCulture, GetString("UnableToFindServices"), p0, p1, p2); + return string.Format(CultureInfo.CurrentCulture, GetString("UnableToFindServices"), p0, p1, p4); } /// diff --git a/src/Microsoft.AspNet.Mvc.Core/Resources.resx b/src/Microsoft.AspNet.Mvc.Core/Resources.resx index 9f56f1705f..16d7ca2608 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Resources.resx +++ b/src/Microsoft.AspNet.Mvc.Core/Resources.resx @@ -208,7 +208,7 @@ An unescaped '[' token is not allowed inside of a replacement token. Use '[[' to escape. - Unable to find the required services. Please add all the required services by calling '{0}' inside the call to '{1}' or '{2}' in the application startup code. + Unable to find the required services. Please add all the required services by calling '{0}.{1}' inside the call to '{2}' in the application startup code. Action: '{0}' - Template: '{1}' diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Builder/MvcApplicationBuilderExtensionsTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Builder/MvcApplicationBuilderExtensionsTest.cs new file mode 100644 index 0000000000..72d851e3ae --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Builder/MvcApplicationBuilderExtensionsTest.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNet.Builder; +using Moq; +using Xunit; + +namespace Microsoft.AspNet.Mvc.Core.Builder +{ + public class MvcApplicationBuilderExtensionsTest + { + [Fact] + public void UseMvc_ThrowsInvalidOperationException_IfMvcMarkerServiceIsNotRegistered() + { + // Arrange + var applicationBuilderMock = new Mock(); + applicationBuilderMock + .Setup(s => s.ApplicationServices) + .Returns(Mock.Of()); + + // Act & Assert + var exception = Assert.Throws( + () => applicationBuilderMock.Object.UseMvc(rb => { })); + + Assert.Equal( + "Unable to find the required services. Please add all the required services by calling " + + "'IServiceCollection.AddMvc' inside the call to 'ConfigureServices(...)' " + + "in the application startup code.", + exception.Message); + } + } +} diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Internal/MvcServicesHelperTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Internal/MvcServicesHelperTests.cs deleted file mode 100644 index 3ba3a8997c..0000000000 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Internal/MvcServicesHelperTests.cs +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNet.Mvc.Internal; -using Moq; -using Xunit; - -namespace Microsoft.AspNet.Mvc -{ - public class MvcServicesHelperTests - { - [Fact] - public void MvcServicesHelperThrowsIfServiceIsAbsent() - { - // Arrange - var services = new Mock(); - services.Setup(o => o.GetService(typeof(IEnumerable))) - .Returns(new List()); - var expectedMessage = "Unable to find the required services. Please add all the required " + - "services by calling 'IServiceCollection.AddMvc()' inside the call to 'IApplicationBuilder.ConfigureServices(...)' " + - "or 'IApplicationBuilder.UseMvc(...)' in the application startup code."; - - // Act & Assert - var ex = Assert.Throws( - () => MvcServicesHelper.ThrowIfMvcNotRegistered(services.Object)); - Assert.Equal(expectedMessage, ex.Message); - } - - [Fact] - public void MvcServicesHelperDoesNotThrowIfServiceExists() - { - // Arrange - var services = new Mock(); - var expectedOutput = new MvcMarkerService(); - services.Setup(o => o.GetService(typeof(MvcMarkerService))) - .Returns(expectedOutput); - - // Act & Assert (does not throw) - MvcServicesHelper.ThrowIfMvcNotRegistered(services.Object); - } - } -} \ No newline at end of file