Skip to content

Commit

Permalink
Added sanity check to RegisterMvcControllers.
Browse files Browse the repository at this point in the history
In case the user forgot to call services.AddMvc or services.AddMvcCore,
an expressive exception will be thrown when the user calls
RegisterMvcControllers, since it requires the ASP.NET MVC infrastructure
to be set up. Related to #391.
  • Loading branch information
dotnetjunkie committed Mar 22, 2017
1 parent 964717d commit bedf78d
Showing 1 changed file with 15 additions and 1 deletion.
Expand Up @@ -57,7 +57,21 @@ public static class SimpleInjectorAspNetCoreMvcIntegrationExtensions
Requires.IsNotNull(container, nameof(container));
Requires.IsNotNull(applicationBuilder, nameof(applicationBuilder));

var manager = applicationBuilder.ApplicationServices.GetRequiredService<ApplicationPartManager>();
var manager = applicationBuilder.ApplicationServices.GetService<ApplicationPartManager>();

if (manager == null)
{
throw new InvalidOperationException(
string.Format(
CultureInfo.InvariantCulture,
"A registration for the {0} is missing from the ASP.NET Core configuration " +
"system. This is most likely caused by a missing call to services.AddMvcCore() or " +
"services.AddMvc() as part of the ConfigureServices(IServiceCollection) method of " +
"the Startup class. A call to one of those methods will ensure the registration " +
"of the {1}.",
typeof(ApplicationPartManager).FullName,
typeof(ApplicationPartManager).Name));
}

var feature = new ControllerFeature();
manager.PopulateFeature(feature);
Expand Down

0 comments on commit bedf78d

Please sign in to comment.