light-inject

Service is always null using lightinject


I'm trying to register some services on a base controller that will available per request.

public interface IInstagramApiService
{
}

public interface IApiServiceConfiguration
{
    string CacheKeyPrefix { get; }
}

Then I have some concrete implementations of these

public class InstagramApiService : IInstagramApiService
{
   IApiServiceConfiguration ApiServiceConfiguration { get; set; }

   public InstagramApiService(IApiServiceConfiguration apiServiceConfiguration)
   {
       ApiServiceConfiguration = apiServiceConfiguration;
   }

}

public class ApiServiceConfiguration : IApiServiceConfiguration
{
    public string CacheKeyPrefix
    {
        get;
        set;
    }
}

Then I have base controller that I want to register this service too;

public class BaseController : Controller
{
    IInstagramApiService InstagramApiService { get; set; }        
}

So in my app start up I have this initialization;

var instagramApiServiceConfiguration = new ApiServiceConfiguration() { 
     CacheKeyPrefix = "The Cache Key Prefix",
};

container.Register<IInstagramApiService>((factory) => new InstagramApiService(instagramApiServiceConfiguration), new PerRequestLifeTime());

But when I check the InstagramApiService on the BaseController it's always null. What is the correct way to set this up so I will always have the InstagramApiService ready to use within my controllers?


Solution

  • Turns out I was missing a simple public modifier on my InstagramApiService inside the BaseController.

    public class BaseController : Controller
    {
        public IInstagramApiService InstagramApiService { get; set; }        
    }