asp.netasp.net-web-apidependency-injectionunity-containerlazycache

Injecting into constructor with 2 params is not working


I have a ASP .Net Web API controller that I want to take 2 parameters. The first one is an EF context and the second being a caching interface. If I just have the EF context the constructor gets called, but when I add the caching interface I get the error:

An error occurred when trying to create a controller of type 'MyV1Controller'. Make sure that the controller has a parameterless public constructor.

private MyEntities dbContext;
private IAppCache cache;

public MyV1Controller(MyEntities ctx, IAppCache _cache)
{
     dbContext = ctx;
     cache = _cache;
}

My UnityConfig.cs

public static void RegisterTypes(IUnityContainer container)
{
    // TODO: Register your types here
    container.RegisterType<MyEntities, MyEntities>();
    container.RegisterType<IAppCache, CachingService>();
}

I would expect that Entity now knows about both types when a request is made for MyV1Controller function it should be able to instantiate an instance since that constructor takes types it knows about but that's not the case. Any idea why?

[EDIT] Note that I created my own class (IConfig) and registered it and add it to the constructor and it worked, but whenever I try to add the IAppCache to my constructor and make a request to the API I get the error telling me it can't construct my controller class. The only difference that I see is the IAppCache isn't in my projects namespace because it's a 3rd party class but that shouldn't matter from what I understand.

Here are the constructors for CachingService

public CachingService() : this(MemoryCache.Default) { } 

public CachingService(ObjectCache cache) { 
    if (cache == null) throw new ArgumentNullException(nameof(cache)); 
    ObjectCache = cache; 
    DefaultCacheDuration = 60*20; 
}

Solution

  • Check the IAppCacheimplementation CachingService to make sure that the class is not throwing any exception when initialized. that parameterless exception is the default message when an error occurs while trying to create controllers. It is not a very useful exception as it does not accurately indicate what the true error was that occurred.

    You mention that it is a 3rd party interface/class. It could be requesting a dependency that the container does not know about.

    Referencing Unity Framework IoC with default constructor

    Unity is calling the constructor with the most parameters which in this case is...

    public CachingService(ObjectCache cache) { ... }
    

    As the container know nothing about ObjectCache it will pass in null which according to the code in the constructor will throw an exception.

    UPDATE:

    Adding this from comments as it can prove useful to others.

    container.RegisterType<IAppCache, CachingService>(new InjectionConstructor(MemoryCache.Default));
    

    Reference here Register Constructors and Parameters for more details.