Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
[Fixes #3928] Ensure we throw at UseMvc during application startup if…
Browse files Browse the repository at this point in the history
… AddMvc wasn't called
  • Loading branch information
javiercn committed Jan 14, 2016
1 parent e078259 commit b50923a
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -77,7 +79,15 @@ public static IApplicationBuilder UseMvc(

// 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",
nameof(IServiceCollection),
nameof(MvcCoreServiceCollectionExtensions.AddMvcCore),
"ConfigureServices(...)"));
}

var routes = new RouteBuilder(app)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ private void EnsureServices(HttpContext context)

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<IActionContextAccessor>();
Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.AspNet.Mvc.Core/Internal/MvcMarkerService.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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 <see cref="IServiceCollection"/> before MVC is configured.
/// </summary>
public class MvcMarkerService
{
Expand Down
30 changes: 0 additions & 30 deletions src/Microsoft.AspNet.Mvc.Core/Internal/MvcServicesHelper.cs

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Mvc.Core/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@
<value>An unescaped '[' token is not allowed inside of a replacement token. Use '[[' to escape.</value>
</data>
<data name="UnableToFindServices" xml:space="preserve">
<value>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.</value>
<value>Unable to find the required services. Please add all the required services by calling '{0}.{1}' or '{2}.{3}' inside the call to '{4}' in the application startup code.</value>
</data>
<data name="AttributeRoute_DuplicateNames_Item" xml:space="preserve">
<value>Action: '{0}' - Template: '{1}'</value>
Expand Down
Original file line number Diff line number Diff line change
@@ -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_IfRoutingMarkerServiceIsNotRegistered()
{
// Arrange
var applicationBuilderMock = new Mock<IApplicationBuilder>();
applicationBuilderMock
.Setup(s => s.ApplicationServices)
.Returns(Mock.Of<IServiceProvider>());

// Act & Assert
var exception = Assert.Throws<InvalidOperationException>(
() => applicationBuilderMock.Object.UseMvc(rb => { }));

Assert.Equal(
"Unable to find the required services. Please add all the required services by calling " +
"'IServiceCollection.AddMvc' or 'IServiceCollection.AddMvcCore' inside the call to " +
"'ConfigureServices(...)' in the application startup code.",
exception.Message);
}
}
}

This file was deleted.

0 comments on commit b50923a

Please sign in to comment.