【问题标题】:How can I use Ninject v3 in asp.net Web Forms?如何在 asp.net Web 表单中使用 Ninject v3?
【发布时间】:2013-03-14 00:14:38
【问题描述】:

我正在尝试在基于 Jason 答案的 asp.net webforms 应用程序中使用 ninject 版本 3

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

但它不起作用?

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,这意味着没有发生注入。

【问题讨论】:

  • 那么会发生什么,还是不会发生?

标签: asp.net ninject ninject.web


【解决方案1】:

我得到它的工作我删除了 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; }

为我工作。

【讨论】:

    猜你喜欢
    • 2011-11-20
    • 2020-10-29
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    相关资源
    最近更新 更多