2

Hello: I've been successfully able to use Ninject with MVC 3/4 and the WCF Extensions. I am now trying to do a simple implementation with WebForms and ran into a challenge. The steps:

  1. I downloaded the Ninject.Web from NuGet and installed into my WebForms project
  2. I edited the NinjectWebCommon.cs -> CreateKernel method:

            kernel.Bind<ICasePresenter>().To<Presenter.CasePresenter>();
    
  3. I edited my user control:

    [Inject]
    private ICasePresenter presenter;
    
    public CaseSummaryControl(ICasePresenter presenter)
    {
        this.presenter = presenter;
    }
    

I can see the breakpoint hit my binding but then I get the error that the user control does not contain a constructor that takes 0 arguments. It's not injecting my instance (even if I add a constructor with no arguments, the private var ends up being null.

Thanks in advance!

mquickel
  • 380
  • 3
  • 20

2 Answers2

1

You are using field injection instead of constructor injection. Just move the Inject attribute to your constructor:

private ICasePresenter presenter;

[Inject]
public CaseSummaryControl(ICasePresenter presenter)
{
    this.presenter = presenter;
}

or add a constructor without the parameter:

[Inject]
private ICasePresenter presenter;

public CaseSummaryControl(ICasePresenter presenter)
{
    this.presenter = presenter;
}

public CaseSummaryControl()
{

}

That way you can keep the constructor with the parameter for unit testing.

The downside is you may need to add null-checks when you use this.presenter since you now have a path to create a CaseSummaryControl without specifying an ICasePresenter.

Read the Ninject documenation on the various injection patterns.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • As the documentation states, I shouldn't have to use the [Inject] attribute at all (this is my previous experience with it). If I exclude it, though, no breakpoint is hit in CreateKernel(). I tried moving it to the constructor as you provided in your example and the breakpoint is hit, but I still get the no constructor error. – mquickel Nov 01 '13 at 13:55
  • According to http://stackoverflow.com/questions/14323489/argument-not-specified-for-parameter-using-ninject-in-web-forms, Web Forms does not support constructor injection, only field injection. Any confirmations on that? Field injection seems to work... – mquickel Nov 01 '13 at 14:56
  • @MQuickel Added an alternate solution. – D Stanley Nov 01 '13 at 15:13
0

ASP.NET Webforms supports mostly PROPERTY based injections. If you are using a constructor to define Injection then it simply throws error.

Here are the steps that you can follow as per today documentation

  1. Install Ninject using Package Manager Console by typing Install-Package Ninject.Web.
  2. Ninject should be installed now and you'll see a file named NinjectWebCommon.cs in the App_Start folder. Simply put your bindings there as already written by questioner.
  3. Rather than creating a constructor create a public property in your calling class / method and use [Inject] attribute above it.

Compile your Application and dependencies should be created by now automatically.

You can see snapshot demonstration of these steps in another post Using Ninject v3 in ASP.NET Web Forms

Community
  • 1
  • 1
vibs2006
  • 6,028
  • 3
  • 40
  • 40