2

I have an WebApi application that contains some controllers (they are registered using the extension method RegisterApiControllers). This application references another assembly that contains other controllers that I don't want to expose(I have checked that they are not registered in the container). It happens that both have an OrderController, and when I try to access the /api/Order url, I get an exception "Multiple types were found that match the controller named 'order'." and the stack trace shows that I was in DefaultHttpControllerSelector.

I have seen that AutofacControllerFactory used to exist and there was even a ConfigureWebApi that registered it, but it is not anymore present in the default branch.(you can see it here http://alexmg.com/post/2012/03/09/Autofac-ASPNET-Web-API-(Beta)-Integration.aspx)

It seems also that we can not filter the namespace of the route definition in WebApi (it is possible to MVC).

So any idea on how I can use only the Controller registered in my Autofac container and not use the DefaultHttpControllerSelector that seems to scan all referenced assemblies to discover controller?

Thanks

Dave
  • 1,835
  • 4
  • 26
  • 44

1 Answers1

3

The problem is that registering the controller with autofac is not really related to the routing process. Only once the routing process has identified which controller to dispatch to will Autofac be called to resolve the type. It looks like, from digging around in the source, that you would need to write a replacement IHttpControllerSelector in order to handle two controllers with the same name. (which really sucks BTW).
You might be able replace the DefaultHttpControllerTypeResolver with an instance that is passed a predicate that filters out the controllers from the assembly that you want to ignore. It's a bit of a kludgy solution but might work.

Actually, you might be able to replace the DefaultHttpControllerTypeResolver completely with one that is based on registrations in your Autofac container. It is a very simple interface, so as long as Autofac have a some kind of discovery mechanism, you should be golden.

   public interface IHttpControllerTypeResolver
    {
      ICollection<Type> GetControllerTypes(IAssembliesResolver assembliesResolver);
    }
Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • It works. Not sure I have been able to make the best HttpControllerTypeResolver but it solves my issue. I have been struggling with listing all controller register inside an autofac container. – Dave May 08 '13 at 00:05
  • 1
    @Dave: Sharing your actual solution would be a neat move ;) – Torben Koch Pløen Dec 24 '14 at 02:52