Skip to content

Commit bedf78d

Browse files
committed
Added sanity check to RegisterMvcControllers.
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.
1 parent 964717d commit bedf78d

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/SimpleInjector.Integration.AspNetCore.Mvc/SimpleInjectorAspNetCoreMvcIntegrationExtensions.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,21 @@ public static void RegisterMvcControllers(this Container container,
5757
Requires.IsNotNull(container, nameof(container));
5858
Requires.IsNotNull(applicationBuilder, nameof(applicationBuilder));
5959

60-
var manager = applicationBuilder.ApplicationServices.GetRequiredService<ApplicationPartManager>();
60+
var manager = applicationBuilder.ApplicationServices.GetService<ApplicationPartManager>();
61+
62+
if (manager == null)
63+
{
64+
throw new InvalidOperationException(
65+
string.Format(
66+
CultureInfo.InvariantCulture,
67+
"A registration for the {0} is missing from the ASP.NET Core configuration " +
68+
"system. This is most likely caused by a missing call to services.AddMvcCore() or " +
69+
"services.AddMvc() as part of the ConfigureServices(IServiceCollection) method of " +
70+
"the Startup class. A call to one of those methods will ensure the registration " +
71+
"of the {1}.",
72+
typeof(ApplicationPartManager).FullName,
73+
typeof(ApplicationPartManager).Name));
74+
}
6175

6276
var feature = new ControllerFeature();
6377
manager.PopulateFeature(feature);

0 commit comments

Comments
 (0)