InRequestScope not working (as expected) in Web API

2,299 views
Skip to first unread message

Colin Young

unread,
Jan 30, 2014, 11:58:01 AM1/30/14
to nin...@googlegroups.com
Using Ninject.Web.WebApi.3.0.2-unstable-9018, Ninject.3.0.2-unstable-9051, and Ninject.Web.Common.3.0.0.7. We are trying to configure a single database connect context like so:

kernel.Bind<ISqlServerConnectionContextIConnectionContext>().ToMethod(x =>
            {
                var newConnectionContext = new SqlServerConnectionContext(connectionString);
                newConnectionContext.ProductID = productID;
                newConnectionContext.Open();
                return newConnectionContext;
            }).InRequestScope();

Unfortunately what we actually get is a new context object for every single object that needs one for the same request, so 21 connections per request. If I switch to InSingletonScope, we get the same context for every object, but I'm also AFAIK getting that same connection on every request, which is not at all what we need.

When I run integration test with in-memory hosting of my API, the test run. If I try to access the service hosted in IIS, it fails with "Method 'get_RequestScope' in type 'Ninject.Web.WebApi.NinjectWebApiHttpApplicationPlugin' from assembly 'Ninject.Web.WebApi, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7' does not have an implementation." previously reported at https://groups.google.com/d/msg/ninject/rC2vhj8yvBU/NAIkNA-QrAAJ and https://groups.google.com/d/msg/ninject/JMLDO396j-w/cOfI7gi20vkJ

Any guidance towards getting Ninject to work with our WebAPI version 4 would be appreciated. At this point I have to assume that Ninject simply isn't ready to support Web API and I should start implementing my plan B.

Thanks.

Colin

Remo Gloor

unread,
Jan 30, 2014, 12:07:38 PM1/30/14
to nin...@googlegroups.com

You need either Ninject.Web.WebApi.WebHost or Ninject.Web.WebApi.SelfHost depending on how you host web api

--
You received this message because you are subscribed to the Google Groups "ninject" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ninject+u...@googlegroups.com.
To post to this group, send email to nin...@googlegroups.com.
Visit this group at http://groups.google.com/group/ninject.
For more options, visit https://groups.google.com/groups/opt_out.

Colin Young

unread,
Jan 30, 2014, 1:38:06 PM1/30/14
to nin...@googlegroups.com
I installed Ninject.Web.WebApi.WebHost 3.0.2-unstable-9018 (that's the latest version that still works with API 4) in a test project via nuget and it doesn't appear to have made any changes to the project references and there are no assemblies in the packages folders.

Daniel Bobbitt

unread,
Jan 30, 2014, 1:38:14 PM1/30/14
to nin...@googlegroups.com
OMG I've been banging my head against the wall all day on this issue and this finally got it to work. I have mixed feelings knowing that I literally could not have solved this issue an hour ago, since I never saw a mention of Ninject,Web,WebApi.WebHost/SelfHost anywhere until this post.

This is the final combination of packages I installed that got it to work for me:
Ninject 3.0.1.10
Ninject.Web.Common 3.0.0.7
Ninject.Web.WebApi 3.0.2-unstable-9022
Ninject.Web.WebApi.Selfhost 3.0.2-unstable-9017

I'm running localhost currently, fingers crossed that WebHost will work as well when I get to that stage of development. 
A warning, it did not work when I had both WebHost & SelfHost installed at the same time.



On Thursday, January 30, 2014 12:07:38 PM UTC-5, Remo Gloor wrote:

Colin Young

unread,
Jan 30, 2014, 2:09:07 PM1/30/14
to nin...@googlegroups.com
If I install the self-host package, I do see a new reference added to the project and the nuget packages folder is populated under the lib folder with assemblies for the newly added package. The same is not true when I add WebHost. There is no reference added to the project, not are there assemblies in the lib folder for the nuget package.

Unfortunately the actual project I am attempting to configure does not use nuget, so I need to understand exactly what is being done in my demo project so I can recreate it manually.

Daniel Bobbitt

unread,
Jan 30, 2014, 3:29:35 PM1/30/14
to nin...@googlegroups.com
What could I tell you that would be helpful for this? Installing WebHost via "PM> Install-Package Ninject.Web.WebApi.WebHost -Pre" adds references and assemblies for me. I don't know what would be useful to know, though. I could paste the contents of my packages.config after installation or the output from the Package Manager Console?

Colin Young

unread,
Jan 30, 2014, 3:53:09 PM1/30/14
to nin...@googlegroups.com
Thanks, but I think the issue is that the versions I'm downloading (we're still on MVC 4) don't seem to have anything included in them. I think there is something wrong with the older unstable releases, but since the latest requires dependencies we aren't ready to update yet, I'm stuck with them.

Daniel Bobbitt

unread,
Jan 30, 2014, 4:32:26 PM1/30/14
to nin...@googlegroups.com
Sorry to have given everyone false hope, I'm actually just dumb as hell.

Installing Ninject.Web.WebApi.WebHost overwrites NinjectWebCommon, and when I uninstalled it, it removed NinjectWebCommon. I was so overjoyed that it successfully ran when I hit F5 I failed to notice that it was working because it had removed the file with the exception in it.

I didn't notice it was gone until trying to piece together why nothing was getting injected.

You are also 100% correct that installing Ninject.Web.WebApi.WebHost DOES NOT include a .dll at all.

I'm having a bad day.

Remo Gloor

unread,
Jan 30, 2014, 5:00:01 PM1/30/14
to nin...@googlegroups.com

WebHost does not add any assemblies that’s correct. It integrates Ninject into the solution by adding Ninject to the App_Start of the project.

 

Your problem could be that the current prerelease is built against WebApi 2. Since you have MVC4 I assume you are using WebApi 1.0. In that case you can either add assembly rebinds back to WebApi 1 assembly versions (I didn’t check if that works) or download the sources and build against WebApi 1.

 

Older versions of the WebApi had a problem with the nuget deployment. Also have a look at the Sample Applications on github.

Daniel Bobbitt

unread,
Jan 30, 2014, 6:09:31 PM1/30/14
to nin...@googlegroups.com
I am using Web Api 2 actually.

I am able to get it run without exception by using these packages (the latest prerelease versions of all of these packages):
Ninject.3.0.2-unstable-9051
Ninject.Web.Common.3.0.2-unstable-9015
Ninject.Web.WebApi.30.2-unstable-9022

It runs, creates the kernel, registers the services, but injection does not work, at all.

If I install the Ninject.Web.WebApi.WebHost 3.0.2-unstable-9022 package, it has no effect on the application. Still runs without exception, but injection does not occur.

I also tried uninstalling WebHost and trying SelfHost (Ninject.Web.WebApi.Selfhost 3.0.2-unstable-9022).

That causes this exception to be thrown at the line "bootstrapper.Initialize(CreateKernel)" (after the kernel is created):

An exception of type 'Ninject.ActivationException' occurred in Ninject.dll but was not handled in user code
Additional information: Error activating HttpSelfHostConfiguration using implicit self-binding of HttpSelfHostConfiguration
Several constructors have the same priority. Please specify the constructor using ToConstructor syntax or add an Inject attribute.

Constructors:
HttpSelfHostConfiguration(string baseAddress)
HttpSelfHostConfiguration(Uri baseAddress)


Activation path:
  1) Request for HttpSelfHostConfiguration
Suggestions:
  1) Ensure that the implementation type has a public constructor.
  2) If you have implemented the Singleton pattern, use a binding with InSingletonScope() instead.


This is not a binding of my creation, and the error persists even if I remove all of my bindings.

Colin Young

unread,
Feb 3, 2014, 2:56:45 PM2/3/14
to nin...@googlegroups.com
Here's what I've done:
  1. Create a brand-new test project in Visual Studio 2012. Choose MVC then WebAPI and end up with a buildable sample application on MVC4/WebAPI 1.
  2. Upgrade to MVC 5/WebAPI 2 following details at http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2. End up with a buildable project.
  3. Install Ninject via Install-Package Ninject.Web.WebApi.WebHost -Pre
    1. End up with a non-building project (IDocumentProvider not defined) and a bunch of new references, including AspNetWebApi that was released in August 2012.
It appears that Ninject.Web.WebApi.WebHost -> Ninject.Web.WebApi -> AspNetWebApi -> Microsoft.AspNet.WebApi (v < 4.1). That last dependency appears to be pulling in a very old Web API, which pre-dates the help pages.

Daniel Bobbitt

unread,
Feb 3, 2014, 4:05:39 PM2/3/14
to nin...@googlegroups.com
I abandoned trying to get the Ninject.Web.WebApi package to work. I burned an entire day on it with nothing to show.

I ended up going to other route, that you've probably googled up the same as I did. I used this tutorial: http://michaelhubele.com/post/2013/06/06/setting-ninject-aspnet-web-api , using only the stable Ninject packages (Ninject, Ninject.Web.Common, Ninject.MVC3)

It works perfectly, and it took me 10 minutes to get set up, including the time it took to uninstall all of the Ninject.Web.WebApi and other unstable packages.

The only downside to this (that I've encountered) is you can't bind Filters. 

Colin Young

unread,
Feb 4, 2014, 8:53:55 AM2/4/14
to nin...@googlegroups.com
I'll give that a try, but I think I've been down that road before, at least partially. Do you know if InRequestScope is working properly for that setup?

Daniel Bobbitt

unread,
Feb 4, 2014, 11:28:49 AM2/4/14
to nin...@googlegroups.com
I looked but wasn't able to try it out to diagnostic satisfaction in my application. I can't reliably say one way or another, because my project is not set up in a way that can use that functionality.

Colin Young

unread,
Feb 5, 2014, 3:06:23 PM2/5/14
to nin...@googlegroups.com
Pretty sure it's not handling the InRequestScope properly, and I recall trying previously to implement a custom one, without much luck. As I said, I've been down this road before with Ninject, with much the same results. I think I'm going to fix the problem by going back to Autofac next week when I've got some time to do a proof-of-concept with our existing projects.

Remo Gloor

unread,
Feb 6, 2014, 12:23:48 PM2/6/14
to nin...@googlegroups.com

Just verified that everything works properly:

1.       Create new WebApi 1.0 using VS Wizard

2.       Changed:

WebApiConfig.Register(GlobalConfiguration.Configuration);

to

GlobalConfiguration.Configure(WebApiConfig.Register);

3.       Uninstall FixedDisplayModes NuGet

Uninstall-Package -Id Microsoft.AspNet.Mvc.FixedDisplayModes

4.       Update all NuGetPackages to latest

5.       Install Ninject.Web.WebApi.WebHost –pre

6.       Change ValuesController to require dependency

7.       Configure Ninject with a binding for the dependency InRequestScope

8.       Run and Navigate to /api/Values

 

Results:

·         A new instance of the dependency is injected for each call.  

·         The depencency is disposed at the end of the call

 

I really can’t reproduce your problems. Don’t know what you are doing differently.

 

The only thing that I found is that you can’t add a dependency to the HomeController because it is MVC and not WebApi. To solve this  you have to install Ninject.Mvc.

Also I don’t know why you think it references MVC 4.1. Following all links on nuget.org shows that it does not and it doesn’t install it either on my machine. Seems that nuget is doing something odd in your case. E.g. installing another than the latest package or taking an old nuspec.

Colin Young

unread,
Feb 6, 2014, 2:47:33 PM2/6/14
to nin...@googlegroups.com
I appreciate you taking the time to check in on this thread, and attempt to reproduce the problem.

If I'm understanding things correctly, in step 4 you are going to end up with Web API 2/MVC 5.1. I'm not sure where I found the MVC 4.1 reference, but I don't see it now, however I still I cannot build my demo app (I haven't even got around to trying to introduce dependencies) due to missing namespaces in System.Web.Http. I am seeing a lot of references to 'beta' packages (e.g. AspNetWebApi/4.0.20126.16343 which, according to nuget, Ninject.Web.WebApi 3.0.0.2 depends on). The same or very similar thing has happened twice with 2 separate new MVC projects. We are not running a private nuget repository here, I am downloading directly from nuget.org.

All this trouble is in a project where I have the luxury of using Nuget to manage my dependencies. Microsoft's versioning isn't helping matters here (various assemblies with versions of 2.0.0.0, 4.0.0.0, 5.1.0.0, 3.0.0.0, etc. all seem to be part of or used by MVC 5.1). The only thing I can think of at this point is that I am using Visual Studio 2012, not 2013 and maybe that is having some impact since I do not have the latest MVC templates that came with 2013 and therefore am starting from an MVC 4 application.

I'm not having this much trouble with any other dependency in our project, so at this point I'm just using ninject for MVC 3 with MVC 4, singleton scope for my connections (that at least allows the unit and integration tests to run without exhausting database connections) which will get my productive until Monday, and then fixing the problem then.

To unsubscribe from this group and stop receiving emails from it, send an email to ninject+unsubscribe@googlegroups.com.


To post to this group, send email to

Remo Gloor

unread,
Feb 6, 2014, 4:01:46 PM2/6/14
to nin...@googlegroups.com

Now I see the problem. You are not using the latest version of Ninject.Web.WebApi

 

This is what you have to use:

 

http://www.nuget.org/packages/Ninject.Web.WebApi.WebHost/3.0.2-unstable-9022

http://www.nuget.org/packages/Ninject.Web.WebApi/3.0.2-unstable-9022

 

Those packages are built against Web Api 2.0 (5.0). But are compatible with 2.1 (5.1)

To unsubscribe from this group and stop receiving emails from it, send an email to ninject+u...@googlegroups.com.


To post to this group, send email to

--

You received this message because you are subscribed to the Google Groups "ninject" group.

To unsubscribe from this group and stop receiving emails from it, send an email to ninject+u...@googlegroups.com.

Colin Young

unread,
Feb 10, 2014, 9:59:22 AM2/10/14
to nin...@googlegroups.com
Yes. According to nuget, that's what I do have:

Ninject.Web.WebApi.WebHost 3.0.2-unstable-9022 (prerelease). The other stuff was a bit of a mess, but even after upgrading, I'm still not able to get things to build.

To unsubscribe from this group and stop receiving emails from it, send an email to ninject+unsubscribe@googlegroups.com.


To post to this group, send email to

Remo Gloor

unread,
Feb 11, 2014, 3:59:00 AM2/11/14
to nin...@googlegroups.com

It seems you messed up your assembly references. Try to remove everything from you packages.config, package folder and assembly references and get it again.

Also download https://github.com/ninject/Ninject.Web.WebApi/tree/master/src/SampleApplication and make sure you have everything like there.

To unsubscribe from this group and stop receiving emails from it, send an email to ninject+u...@googlegroups.com.
To post to this group, send email to nin...@googlegroups.com.
Visit this group at http://groups.google.com/group/ninject.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "ninject" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ninject+u...@googlegroups.com.
To post to this group, send email to nin...@googlegroups.com.
Visit this group at http://groups.google.com/group/ninject.
For more options, visit https://groups.google.com/groups/opt_out.

Juan José Luis García

unread,
Nov 13, 2014, 11:07:40 AM11/13/14
to nin...@googlegroups.com
It's November. I have the latest version of the package and I have the same issue. I followed the steps. I have to uninstall the package and follow the link in the previous post to do it manually.

Juan José Luis García

unread,
Nov 13, 2014, 12:51:22 PM11/13/14
to nin...@googlegroups.com
Sorry I didn't realise that Ninject.Mvc3 already works with webapi. This package is not stable and we should use Ninject.Mvc3 instead.
Reply all
Reply to author
Forward
0 new messages