0
使用DI容器並使用StructureMap創建了一個ASP.Net MVC3應用程序。 一切工作在控制器方法。但我怎麼能爲一個global.asax方法?在Global.asax的方法中使用依賴注入
這裏我設置的依賴解析器,的Application_Start global.ascx
protected void Application_Start()
{
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
LoadApplicationMetaData(?,?,?);
}
private void LoadApplicationMetaData(IMetaDataService metaService, ITenantService tenantService, ICacheStorage store)
{
store.Add("TenantMeta", tenantService.GetAllTenants());
}
public class TenantService : ITenantService
{
private readonly ITenantRepository TenantRepsitory;
public TenantService(ITenantRepository _tenantRepository)
{
TenantRepsitory = _tenantRepository;
}
}
在此線之下我該怎麼辦鬆散的方法調用連接。
**LoadApplicationMetaData(?,?,?); what should be passed**
注:TenantService類期待ITenantRepository
我不我不太瞭解StructureMap,但我認爲你可以從你的容器中獲取實例。 (因爲你已經註冊) –
@FelipeOriani謝謝,閱讀後http://stackoverflow.com/questions/5512040/ninject-ing-a-dependency-in-global-asax我找到了從我的DI獲取實例的方式容器。它爲我工作! –