JasperFx / lamar

Fast Inversion of Control Tool and Successor to StructureMap
https://jasperfx.github.io/lamar
MIT License
572 stars 119 forks source link

MVC Endpoints not seen when controllers are registered from a ServiceRegistry #189

Closed blemasle closed 4 years ago

blemasle commented 5 years ago

This PR adds a (currently failling) test demonstrating that MVC endpoints are not seen when the MvcServiceCollectionExtensions.AddControllers method is called from a ServiceRegistry.

In this test, two other working cases are commented out for demonstration and/or comparison purpose. I'm not really sure this is specifically related to Lamar itself though, it might as well be an issue with Asp.Net Core.

jeremydmiller commented 5 years ago

@blemasle What are you expecting to have happen here? I looked into what that extension method does, and it doesn't actually go find controllers at that point, only verifies that a factory is registered.

Are you seeing this work without Lamar? I haven't dug hard enough into the AspNetCore code, but if they're naive in how they do type scanning, they could have silent failures if any dependency is missing

blemasle commented 5 years ago

@jeremydmiller From my perspective, there should be no difference between calling AddControllers from ConfigureServices, ConfigureContainer or from a ServiceRegistry.

The only case where controllers are not mapped is when AddControllers is called from ServiceRegistry. I can't find an explanation why this usage should not work/be possible 😕 As it's working when called from ConfigureContainer, it's very confusing/unintuitive it does not from a ServiceRegistry...

I haven't tried with other DI framework to be honest.

jeremydmiller commented 4 years ago

It's an ordering issue and not a Lamar thing at all. We have to beat this one with documentation. Deep, deep into the MVC call stack it's magically looking for a previously registered IWebHostEnvironment service that will tell you the main application assembly. The call to AddControllers() will quietly register all the services, but it doesn't set you up for the controller discovery to work at all. That's why you have to call the IHostBuilder.ConfigureWebHostDefaults() method.

I'm closing this and making a note to update this in the 4.1 documentation. Feel very free to yell at the ASP.Net Core team about this;)