0
我在我的Global.ascx.cs文件中使用来自OnApplicationStarted的服务。有没有办法从那里依赖注入存储库?Ninject从OnApplicationStarted注入依赖项
我的代码:
public class MvcApplication : NinjectHttpApplication
{
//Need to dependency inject this.
private IBootStrapService bootService;
protected override void OnApplicationStarted()
{
//Used to set data such as user roles in database on a new app start.
bootService.InitDatabase();
base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
internal class SiteModule : NinjectModule
{
public override void Load()
{
//I set my bindings here.
Bind<IBootStrapService>().To<BootStrapService>();
Bind<IUserRepository>().To<SqlServerUserRepository>()
.WithConstructorArgument("connectionStringName", "MyDb");
}
}
}
所以我怎么ninject做DI权的内部应用程序启动?正如你所看到的,我在SiteModule
类中设置了我的绑定。
因此,在完成此操作之后,服务将在OnApplicationStarted中的代码执行之前注入? – 2011-04-08 06:20:42
@Lol编码器,不,在这种情况下'bootService'不会自动注入,因为它是Http应用程序的一部分,Ninject不能控制它的实例化。你可以手动注入它。我会更新我的帖子以展示一个例子。 – 2011-04-08 06:24:25