开发者

Can't get DependencyResolver to work with ninject?

开发者 https://www.devze.com 2023-04-05 23:19 出处:网络
I have this code in my bootstap of Ninject : private static IKernel CreateKernel() { var kernel = new StandardKernel();

I have this code in my bootstap of Ninject :

private static IKernel CreateKernel()
{
    var kernel = new StandardKernel();
    RegisterServices(kernel);
    DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
    return kernel;
}

private static void RegisterServices(IKernel kernel)
{
    //TODO: Enregistrer les services ici.. pour ninject!
    kernel.Bind<IContext>().To<MySampleContext>();
    kernel.Bind<ISampleService>().To<SampleService>();
}  

In my global i try this..

 protected virtual void Application_BeginRequest()
        {
            ContextProvider cp = new ContextProvider();
            DbContext context = (DbContext)DependencyResolver.Current.GetService<IContext>();
            cp.SetCurrent(context);
        }

EDIT: Here the full bootstrapper... but its the basic one that come with ninjectt.

public static class NinjectMVC3 
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
        DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
        bootstrapper.Initialize(CreateKernel);
    }

    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        RegisterServices(kernel);
        DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
        return kernel;
    }

    private static void RegisterServices(IKernel kernel)
    {
        //TODO: Enregistrer les services ici.. pour ninject!
        kernel.Bind开发者_开发知识库<IContext>().To<MySampleContext>();
        kernel.Bind<ISampleService>().To<SampleService>();
    }        
}

but it return NULL ? why ?


Use Ninject.MVC3 found on NuGet and read the documentation https://github.com/ninject/ninject.web.mvc/wiki/MVC3

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号