forked from aspnet/Mvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue aspnet#347: Throw a better exception when the user didn't add t…
…he MVC services. Introducing MarkerService for identifying if MvcServices were added.
- Loading branch information
sornaks
committed
Aug 21, 2014
1 parent
eccd25b
commit 791518d
Showing
16 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/Microsoft.AspNet.Mvc.Core/Internal/MvcMarkerService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
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. | ||
/// </summary> | ||
public class MvcMarkerService | ||
{ | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Microsoft.AspNet.Mvc.Core/Internal/MvcServicesHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. 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; | ||
using Microsoft.Framework.DependencyInjection; | ||
|
||
namespace Microsoft.AspNet.Mvc.Internal | ||
{ | ||
/// <summary> | ||
/// Helper class which contains MvcServices related helpers. | ||
/// </summary> | ||
public static class MvcServicesHelper | ||
{ | ||
/// <summary> | ||
/// Throws InvalidOperationException when MvcMarkerService is not present | ||
/// in the list of services. | ||
/// </summary> | ||
/// <param name="services">The list of services.</param> | ||
/// <param name="serviceType">The type of service which needs to be searched for.</param> | ||
public static void ThrowIfMvcNotRegistered(IServiceProvider services) | ||
{ | ||
if (services.GetServiceOrNull(typeof(MvcMarkerService)) == null) | ||
{ | ||
throw new InvalidOperationException(Resources.FormatUnableToFindServices( | ||
"IServiceCollection.AddMvc()", "IBuilder.UseServices(...)", "IBuilder.UseMvc(...)")); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Microsoft.AspNet.Mvc.Core/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
test/Microsoft.AspNet.Mvc.Core.Test/Internal/MvcServicesHelperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. 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<IServiceProvider>(); | ||
services.Setup(o => o.GetService(typeof(IEnumerable<MvcMarkerService>))) | ||
.Returns(new List<MvcMarkerService>()); | ||
var expectedMessage = "Unable to find the required services. Please add all the required " + | ||
"services by calling 'IServiceCollection.AddMvc()' inside the call to 'IBuilder.UseServices(...)' " + | ||
"or 'IBuilder.UseMvc(...)' in the application startup code."; | ||
|
||
// Act & Assert | ||
var ex = Assert.Throws<InvalidOperationException>( | ||
() => MvcServicesHelper.ThrowIfMvcNotRegistered(services.Object)); | ||
Assert.Equal(expectedMessage, ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void MvcServicesHelperDoesNotThrowIfServiceExists() | ||
{ | ||
// Arrange | ||
var services = new Mock<IServiceProvider>(); | ||
var expectedOutput = new MvcMarkerService(); | ||
services.Setup(o => o.GetService(typeof(IEnumerable<MvcMarkerService>))) | ||
.Returns(new List<MvcMarkerService> { expectedOutput }); | ||
|
||
// Act & Assert | ||
Assert.DoesNotThrow(() => MvcServicesHelper.ThrowIfMvcNotRegistered(services.Object)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
test/Microsoft.AspNet.Mvc.FunctionalTests/MvcStartupTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Builder; | ||
using Microsoft.AspNet.TestHost; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNet.Mvc.FunctionalTests | ||
{ | ||
public class MvcStartupTests | ||
{ | ||
private readonly IServiceProvider _provider = TestHelper.CreateServices("AddServicesWebSite"); | ||
private readonly Action<IBuilder> _app = new AddServicesWebSite.Startup().Configure; | ||
|
||
[Fact] | ||
public void MvcThrowsWhenRequiredServicesAreNotAdded() | ||
{ | ||
// Arrange | ||
var expectedMessage = "Unable to find the required services. Please add all the required " + | ||
"services by calling 'IServiceCollection.AddMvc()' inside the call to 'IBuilder.UseServices(...)' " + | ||
"or 'IBuilder.UseMvc(...)' in the application startup code."; | ||
|
||
// Act & Assert | ||
var ex = Assert.Throws<InvalidOperationException>(() => TestServer.Create(_provider, _app)); | ||
Assert.Equal(expectedMessage, ex.Message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">12.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>6a0b65ce-6b01-40d0-840d-eff3680d1547</ProjectGuid> | ||
<OutputType>Web</OutputType> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="$(OutputType) == 'Console'"> | ||
<DebuggerFlavor>ConsoleDebugger</DebuggerFlavor> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="$(OutputType) == 'Web'"> | ||
<DebuggerFlavor>WebDebugger</DebuggerFlavor> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'" Label="Configuration"> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | ||
<DevelopmentServerPort>38820</DevelopmentServerPort> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNet.Builder; | ||
using Microsoft.AspNet.Routing; | ||
|
||
namespace AddServicesWebSite | ||
{ | ||
public class Startup | ||
{ | ||
public void Configure(IBuilder app) | ||
{ | ||
var configuration = app.GetTestConfiguration(); | ||
|
||
// Not calling AddMvc() here. | ||
// The purpose of the Website is to demonstrate that it throws | ||
// when AddMvc() is not called. | ||
|
||
// Add MVC to the request pipeline | ||
app.UseMvc(routes => | ||
{ | ||
routes.MapRoute("areaRoute", | ||
"{area:exists}/{controller}/{action}", | ||
new { controller = "Home", action = "Index" }); | ||
routes.MapRoute("ActionAsMethod", "{controller}/{action}", | ||
defaults: new { controller = "Home", action = "Index" }); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"dependencies": { | ||
"Microsoft.AspNet.Mvc": "", | ||
"Microsoft.AspNet.Mvc.TestConfiguration": "" | ||
}, | ||
"frameworks": { | ||
"net45": { }, | ||
"k10": { } | ||
} | ||
} |