6

下面是 NinjectHttpApplication 配置..

public class MvcApplication : NinjectHttpApplication
{
        public MvcApplication()
        {
            Error += NinjectWebsiteApplication_Error;
        }
}    

protected override IKernel CreateKernel()
{
        var kernel = new StandardKernel();
        kernel.Load<ApplicationConfig>();
        return kernel;
}

void NinjectWebsiteApplication_Error(object sender, System.EventArgs e)
{
        ILogger _iLogger = **//How to get instance of Applogger here from Ninject kernel..**
}

下面是 ApplicationConfig 类...

public class ApplicationConfig : NHibernateNinjectModule
{

    public ApplicationConfig()
    {
     //other settings such as ddl script generation is present here
    }
    public override void Load()
    {                
        base.Load();
        Bind<ILogger>().To<AppLogger>().InSingletonScope();
    }
}
4

1 回答 1

4
void NinjectWebsiteApplication_Error(object sender, System.EventArgs e)
{
     var kernel = CreateKernel();
     ILogger _iLogger = kernel.Get<ILogger>();
}

好的,因为它似乎NinjectHttpApplication将生成的内核对象存储在

public IKernel Kernel
{
    get { return _kernel; }
}

所以你可以将上面重写为

void NinjectWebsiteApplication_Error(object sender, System.EventArgs e)
{
     ILogger _iLogger = Kernel.Get<ILogger>();
}
于 2012-10-04T14:29:55.277 回答