如何在asp.net Web窗体中使用Ninject v3?

时间:2022-08-17 15:16:37

I am trying to use ninject version 3 in asp.net webforms application based on Jason answer

我试图在基于杰森答案的asp.net webforms应用程序中使用ninject版本3

How can I implement Ninject or DI on asp.net Web Forms?

如何在asp.net Web窗体上实现Ninject或DI?

but it is not working?

但它不起作用?

public class Global : NinjectHttpApplication
    {
........
protected override IKernel CreateKernel()
        {
            IKernel kernel = new StandardKernel(new NinjectWebCommon());
            return kernel;
        }
    }


public class NinjectWebCommon : NinjectModule
    {

        public override void Load()
        {
            Bind<IProductRepository>().To<EFProductRepository>();
        }
    }

When I start debugging the CreateKernel never called and I get an exception NullReferenceException in the page that means no injection happened.

当我开始调试从未调用的CreateKernel时,我在页面中得到一个异常NullReferenceException,这意味着没有发生注入。

1 个解决方案

#1


2  

I got it work I removed the global.asax file as I don not need and add the NinjectWebCommon.cs removed the references of ninject and added them again from nuget ( Ninject - Ninject.Web) and added my binding in App_Start/NinjectWebCommon.cs

我得到了它的工作我删除了global.asax文件因为我不需要并添加NinjectWebCommon.cs删除了ninject的引用并再次从nuget(Ninject - Ninject.Web)添加它们并在App_Start / NinjectWebCommon.cs中添加了我的绑定

private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IProductRepository>().To<EFProductRepository>();
        }  

and in the page I used

在我使用的页面中

[Inject]
        public IProductRepository Repository { get; set; }

and worked for me .

并为我工作。

#1


2  

I got it work I removed the global.asax file as I don not need and add the NinjectWebCommon.cs removed the references of ninject and added them again from nuget ( Ninject - Ninject.Web) and added my binding in App_Start/NinjectWebCommon.cs

我得到了它的工作我删除了global.asax文件因为我不需要并添加NinjectWebCommon.cs删除了ninject的引用并再次从nuget(Ninject - Ninject.Web)添加它们并在App_Start / NinjectWebCommon.cs中添加了我的绑定

private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IProductRepository>().To<EFProductRepository>();
        }  

and in the page I used

在我使用的页面中

[Inject]
        public IProductRepository Repository { get; set; }

and worked for me .

并为我工作。