2

The issue was noticed with the call to the extension method container.RegisterWebApiControllers(GlobalConfiguration.Configuration) on the container that's supposed to register the web api controller with the container but didn't. Please note that the web api controllers are defined in a different class library project and plugged at application start up using a custom IAssembliesResolver type.

public static class SimpleInjectorWebApiInitializer
{
    public static void Initialize()
    {
        var container = new Container();

        GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver),
            new ApiAssemblyResolver());

        InitializeContainer(container);

        container.RegisterWebApiControllers(GlobalConfiguration.Configuration);

        container.Verify();

        GlobalConfiguration.Configuration.DependencyResolver = 
            new SimpleInjectorWebApiDependencyResolver(container);
    }

I even tried to call to get the ControllerTypes manually to see what was going on using the code below but it never triggered the GetAssemblies method neither did it return any ControllerTypes.

var controllerTypes = httpConfigServicesContainer.GetHttpControllerTypeResolver()
    .GetControllerTypes(
        GlobalConfiguration.Configuration.Services.GetAssembliesResolver());

I am almost pulling out all my hair as I can't seem to see what I am doing wrong. Thanks for your help in advance.

2
  • Which version of Simple Injector are you using? Commented Feb 17, 2015 at 18:28
  • I'm using version 2.7.2. Commented Feb 17, 2015 at 20:09

2 Answers 2

2

It's hard to be very specific, but here's a list of things that might be causing your controllers to not be registered:

  • Your ApiAssemblyResolver does not return the assembly that holds the controllers.
  • That assembly is a dynamic assembly (meaning that Assembly.IsDynamic returns true). Web API will skip dynamic assemblies.
  • The controller types are internal. Web API only uses public types.
  • The controller types are not classes (but structs).
  • Their type name does not end with "Controller"
  • They don't implement IHttpController.
  • Your IAssembliesResolver isn't registerd correctly (perhaps you are missing a binding redirect, causing your application to reference two versions of Web API). Tip: Check what for type GlobalConfiguration.Configuration.Services.GetAssembliesResolver() actually resolves.

Also try the following:

var controllerTypes = httpConfigServicesContainer.GetHttpControllerTypeResolver()
    .GetControllerTypes(new ApiAssemblyResolver());

Does this result in any controllers? In that case, there is probably something miss with the call to Replace(typeof(IAssembliesResolver).

Sign up to request clarification or add additional context in comments.

3 Comments

I have performed all the checks you mentioned above and everything seems to be accurate. I also tried to return the controllerTypes in the last suggestion and nothing was returned! Vis-a-vis this challenge, I created another solution and replicated the same thing and SHAZAM! it was working there but not in my main project.
@OswaldUmeh: That's really weird, but at least we can exclude a few things here. The problem is not with Simple Injector, since it just uses the configured IHttpControllerTypeResolver that returns no controllers. My advice is to create a new empty project and try to reproduce it from there, or strip your solution to the absolute minimum that reproduces the problem. This will often help you spot the reason. Good luck
Ok and thanks for your assistance thus far. I will post a solution when I find one.
1

The solution lay in the order in which configuration calls were made. I moved every call for configuration involving the IOC container into the Application_Start method of Global.asax file.

Before making the call to GlobalConfiguration.Configure(WebApiConfig.Register), I had already called

GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver), new ApiAssemblyResolver())

to replace the default assemblies resolver. I finally placed the other container web api configuration settings after every other configuration call and it started working like a charm! I.e

var apiIOCContainer = new Container();
SimpleInjectorWebApiInitializer.InitializeContainer(apiIOCContainer);
apiIOCContainer.RegisterWebApiControllers(GlobalConfiguration.Configuration);
GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(apiIOCContainer);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.