UPDATE - Please look at my answer for a link and explanation of the solution to this problem
Before we start, I know this is a very common quest
Shortcut for all this: Download one of the following Nuget Pacakges:
Ninject.MVC3
Ninject.MVC5
That will take care of the binding that you need.
The Problem seems to occur when one uses Visual Studio 2012/2013 with ASP.NET 4.0/4.5. The version of System.Web.Mvc
was set to 4.0.0.0
in the Web.config
file. It did not work.
My solution was to
NinjectWebCommon.cs
copy Dere Jone's NinjectControllerFactory
class into the project:
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory(IKernel kernel)
{
ninjectKernel = kernel;
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return (controllerType == null) ? null : (IController) ninjectKernel.Get(controllerType);
}
}
change the content of Global.asax
to:
public class MvcApplication : NinjectHttpApplication
{
protected override IKernel CreateKernel()
{
IKernel kernel = new StandardKernel();
// kernel.Load(Assembly.GetExecutingAssembly());
// kernel.Bind<ISomeClass>().To<SomeClass>();
return kernel;
}
protected override void OnApplicationStarted()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory(Kernel));
}
}
after that your injections should work.
I was getting this issue but only on Web Service calls. Found my solution on this answer https://stackoverflow.com/a/12379938/129580 below is the sample code provided
public class Service : WebService
{
public IContextProvider ContextProvider {get;set;}
public Service()
{
ContextProvider = DependencyResolver.Current.GetService<IContextProvider>();
}
}
which I then ended up turning into, hope this helps...
/// <summary>
/// Summary description for PartMarking
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class PartMarking : System.Web.Services.WebService
{
private readonly IUnitOfWork _unitOfWork;
public PartMarking()
{
_unitOfWork = DependencyResolver.Current.GetService<IUnitOfWork>();
}
[WebMethod]
public DataSet GetParentConfigurations(string engineModel, string componentCode)
{
var results = _unitOfWork.PartMarking.GetParentSicMarkings(engineModel, componentCode);
return results;
}
}
I tried all these solutions, but none worked.
This is how I resolved it: Deleted all my temp asp.net files. After I'd done this, I got new config errors. I fixed them. The last, and real error came out: new Bootstrapper() was trying to load an older version of a ninject dll. Removed all ninject packages. Installed ninject nuget packages one by one, make triply sure that the correct version is installed.
I tried to set up Ninject with MVC 5. Finally i had to use solution with controller factory first, and solution with binding redirect then. After that all works as expected. Used Local IIS server.