简体   繁体   English

Ninject不将服务注入Web窗体页面

[英]Ninject not injecting service into Web Forms page

I've trying to use Ninject for DI in a combined ASP.NET Web Forms and MVC project. 我试图在结合的ASP.NET Web窗体和MVC项目中使用Ninject for DI。 I installed the following packages (and their dependencies) via NuGet: 我通过NuGet安装了以下软件包(及其依赖项):

Ninject.MVC5 3.2.1.0 Ninject.MVC5 3.2.1.0

Ninject.Web 3.2.1.0 Ninject.Web 3.2.1.0

In ~/App_Start/NinjectWebCommon.cs I register services: ~/App_Start/NinjectWebCommon.cs我注册服务:

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

In MVC controllers, I use constructor injection to retrieve my ITermusRepository implementation: 在MVC控制器中,我使用构造函数注入来检索ITermusRepository实现:

public class Appraisal2013_2014FullController : Controller
{
    ITermusRepository repo { get; set; }

    public Appraisal2013_2014FullController(ITermusRepository Repo)
    {
        repo = Repo;
    }
}

MVC works great, I use the repo later in my action methods to successfully retrieve data. MVC的伟大工程,我用的是repo后来在我的行动方法成功地检索数据。 All is well there. 一切都很好。

In Web Forms, I use attribute injection. 在Web窗体中,我使用属性注入。

public partial class _2013_2014_TERMUS_PaperTermus : BasePage
{
    [Inject]
    ITermusRepository repo { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {               
            var appraisal = repo.LoadByTermusId<Termus2013_2014EndYear>(Request.QueryString["TERMUSID"]);
        }

}

However, repo.LoadByTermusId() call fails with a NullReferenceException because repo is null. 但是, repo.LoadByTermusId()调用失败,并显示NullReferenceException因为repo为null。

System.NullReferenceException: Object reference not set to an instance of an object. System.NullReferenceException:对象引用未设置为对象的实例。

Clearly Ninject is set up correctly at least for MVC. 显然,至少为MVC正确设置了Ninject。 I don't understand why my implementation of ITermusRepository isn't getting injected into my Web Forms code behind. 我不明白为什么我的ITermusRepository实现没有被注入背后的Web Forms代码中。 What can I do to get it to inject it properly? 我该怎么做才能使其正确注入?

I used Jason's answer from How can I implement Ninject or DI on asp.net Web Forms? 我使用了Jason的答案,该答案来自于如何在asp.net Web窗体上实现Ninject或DI? as my pattern for getting this working. 作为我的工作方式。 I don't want to use Joe's answer, as it requires modifying the base class of the global application class, pages, master pages, ASMX services, generic handlers etc. And that appears unnecessary in the current version of Ninject. 我不想使用Joe的答案,因为它需要修改全局应用程序类,页面,母版页,ASMX服务,通用处理程序等的基类。在当前版本的Ninject中这似乎是不必要的。

[Inject]
ITermusRepository repo { get; set; }

needed to be 需要成为

[Inject]
public ITermusRepository repo { get; set; }

That fixed the problems with my .aspx Web Forms pages. 这解决了我的.aspx Web窗体页面的问题。 But it wasn't injecting them into my .ashx generic handlers. 但这并不是将它们注入到我的.ashx通用处理程序中。 Since I don't have many of those, I created a constructor in my handler class and retrieved the service from the kernel. 由于没有很多,所以我在处理程序类中创建了一个构造函数,并从内核中检索了服务。

ITermusRepository repo { get; set;}

public GetPDF()
{
    var kernel = TERMUS.App_Start.NinjectWebCommon.CreateKernel();
    repo = kernel.Get<ITermusRepository>();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM