kanayabhattad / autofac

Automatically exported from code.google.com/p/autofac
Other
0 stars 0 forks source link

Throw exception if DependencyResolver registered without RegisterControllers #437

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
if I use this

    GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

but have not called this 

    builder.RegisterApiControllers(typeof(MvcApplication).Assembly);

OR i have use this 

    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

and have not called this

    builder.RegisterControllers(typeof(MvcApplication).Assembly);

It would be nice if autofac could detect this and throw an execption

It would also be nice if the inverse was detected as well

Original issue reported on code.google.com by simon.cropp@gmail.com on 24 May 2013 at 12:30

GoogleCodeExporter commented 8 years ago
I'm not sure we can do that. Just speaking personally, I know I have 
applications where I explicitly register controllers, so I never call the 
RegisterControllers extension. If we added a checker to ensure it was called, 
then my apps would fail (as, potentially, would other peoples' apps).

We can't really even require that *any* controllers be registered since, 
technically, the default controller activator can fall back and use 
Activator.CreateInstance if the controllers aren't registered. It wouldn't make 
a lot of sense if you *didn't* register your controllers in Autofac, but I'm 
not sure we should *mandate* it by throwing an exception, either.

What I might suggest if you want that behavior in your application:

Derive from AutofacDependencyResolver and, during construction, check to see if 
any controllers are registered; if not, throw. (You'd have to do it on 
dependency resolver construction since there's no hook into the setting of 
resolvers in MVC or WebAPI.) You might have to check for a specific expected 
controller, or you could pass in the assembly and ensure that every controller 
from a given assembly is registered.

I'm going to close this for now; if you have an update or if I'm 
misunderstanding, do let me know.

Original comment by travis.illig on 28 May 2013 at 4:11