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

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

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.

有帮助吗?

解决方案

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

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 .

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top