34

Have a couple of questions regarding the latest version (2.2.1.4) of ninject.

Was trying to Bind a Linq2sql DataContext to a concrete implementation InRequestScope (in a class library project)

Bind<DataContext>().To<MoneywatchDataContext>()

but could not find InRequestScope method

enter image description here

ended up doing this,

Bind<DataContext>().To<MoneywatchDataContext>().InScope(ctx => HttpContext.Current)

Just wanted to find out if:

  1. If this will behave exactly like the InRequestScope Method.
  2. That it will gurantee that when my HTTContext.Current is GC'ed the underlying DataCotext will be disposed as well.
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Raghu
  • 2,678
  • 2
  • 31
  • 38

3 Answers3

50

InRequestScope is available as extension method in Ninject.Web.Common. So if installed earlier through Nuget (Ninject package) now we might need to install additional nuget package Ninject.Web.Common

Lokeshwer
  • 1,139
  • 1
  • 9
  • 14
  • Remeber that you need to install this package on the project that your module class is being configured. I had a problem in which I have installed it in the MVC project but the module where I put `Bind<>().To<>().InRequestScope();` was in the Infrastucure project. I took me a while to realize that. – fabriciorissetto Jun 03 '16 at 17:40
  • That was it! Thanks. – Kevin Amorim Apr 27 '20 at 10:16
35
using Ninject.Web.Common;    // that's most likely all you need
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
23

Most likely you reference a no web version of Ninject. Replace it with the normal version and you will have the InRequestScope extension method.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Remo Gloor
  • 32,665
  • 4
  • 68
  • 98