2013-03-24 62 views
0

我想基於傑森回答如何在asp.net Web窗體中使用Ninject v3?

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

在asp.net web表單應用程序中使用ninject第3版,但它不工作?

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>(); 
     } 
    } 

當我開始調試CreateKernel從來不叫,我得到的,這意味着沒有注射發生的頁面異常的NullReferenceException。

+1

那麼會發生什麼或不發生? – 2013-03-24 16:07:22

回答

2

我得到它的工作我去掉了Global.asax文件,因爲我不唐需要,並添加NinjectWebCommon.cs 去除ninject的引用和的NuGet再次加入他們(Ninject - Ninject.Web)和 加入我的結合在App_Start/NinjectWebCommon.cs

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

,並在頁面我用

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

,併爲我工作。

相關問題