2016-09-03 68 views
0

我想在基本控制器上註冊一些服務,每個請求都可用。服務總是空使用lightinject

public interface IInstagramApiService 
{ 
} 

public interface IApiServiceConfiguration 
{ 
    string CacheKeyPrefix { get; } 
} 

然後,我有這些

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

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

} 

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

然後,我有,我想也註冊該服務基本控制器一些具體的實現;

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

所以在我的應用程序啓動,我有這個初始化;

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

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

但是當我檢查InstagramApiServiceBaseController它總是null。什麼是正確的方法來設置這個,所以我會始終準備好在我的控制器中使用InstagramApiService

回答

0

原來我在BaseControllerInstagramApiService上丟失了一個簡單的public修飾符。

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