I started a new ASP.NET MVC project and I'm using Ninject as my IOC controller.
As far as regular MVC Controllers goes - everything work fine and binding is done to the controller constructor as expected.
I've added a WEB API to my project and did pretty much the same thing. here is the API constructor:
public class DetailsController : ApiController
{
private IClientInfoRetriever _clientInfoRetriever;
public DetailsController(IClientInfoRetriever clientInfoRetriever)
{
_clientInfoRetriever = clientInfoRetriever;
}
.
.
.
// The rest of my methods
}
here is the Ninject binding:
Bind<IClientInfoRetriever>().To<ClientInfoRetriever>();
but when I try to access my API (just putting the URL in the browser for a get action) I get the following error:
An error occurred when trying to create a controller of type 'DetailsController'. Make sure that the controller has a parameterless public constructor.
It's expecting to get an empty constructor, but if I give it an empty constructor to use - it won't be initializing the object I need.
What am I doing wrong? does Ninject support web api?
Thanks
Install the proper Ninject Nuget package for WebAPI:
PM> Install-Package Ninject.Web.WebApi -Version 3.0.0.2
Related
I'm using ASP.NET Core MVC and Web API and trying to consume the internal Web API (done using ApiController, prepare for future cross-platform application use), I saw an answer which doesn't need to use HttpClient or any Http Request features to get data from Web API, refer: Consuming web api controller action from mvc controller
I'm trying to do the similar thing, but my auto generated Web API Controller comes with DBContext parameter, which causing me unable to follow what is mentioned from the link.
Below is what i have in my Controller which caused me unable to follow actions mentioned in the link above:
private readonly MyTestDBContext _context;
public MfgProductsController(MyTestDBContext context)
{
_context = context;
}
If the "MyTestDBContext context" parameter supposed to remain, what should I write in my Controller in order to pass the DBContext in?
Or if there's a way to remove "MyTestDBContext context" from the parameter, how the constructor supposed to change to?
Let the container do its job, just add the controller as a dependency in your controller:
public class MyController : Controller
{
public MyController(MfgProductsController productsController)
{
_productsController = productsController;
}
private readonly MfgProductsController _productsController;
}
It should fill all dependencies for you.
We are migrating from .aspx web application to MVC app.
Below is the problem while migrating:
I have a third party dll which contains static factory method to return instance by given Interface name.
Controller code:
IBus objBus = (IBus) ObjectFactory.GetInstance("IBus") as IBus;
Here, ObjectFactory is from third party dll with definition as below (no more code details are available on GetInstance method).
public class ObjectFactory
{
public ObjectFactory();
public static object GetInstance(string interfaceName);
}
GetInstance method works perfectly in .aspx application - objBus is created successfully.
But the same line (Controller code) when executed in MVC controller returns objBus as null.
Please suggest what could be the problem.
I guess it may be due to difference in architecture and/or page life cyle of .aspx and mvc apps.
Any suggestions on the problem are greatly appreciated.
I think you should reach out to your third party DLL provider and ask for documentation on how to work with ASP.NET MVC.
If I was you I would think about dropping that DLL using Unity for you dependency injection for an MVC Project. All you need to do is in the Application_Start method in the Global.asax use the UnityConfig and DependecyResolver to inject you instances of class in to any of your controller methods.
If you need details check out these great articles.
http://netmvc.blogspot.com/2012/04/dependency-injection-in-aspnet-mvc-4.html
http://dotnetslackers.com/articles/aspnet/Using-Microsoft-Unity-in-ASP-NET-MVC.aspx
http://www.codeproject.com/Articles/99361/How-To-Use-Unity-Container-In-ASP-NET-MVC-Framewor
I've created a default MVC5 web application in Visual Studio 2013, and added in the StructureMap.MVC5 package from NuGet.
I've created an interface ITester and an implementation Tester, and my controller action takes an ITester as a parameter.
But when I run the project I get the error Cannot create an instance of an interface.
According to this is should just work. I also get the same result if I explicitly tell structuremap to use Tester for ITester (rather than relying on the default conventions).
I've used StructureMap.MVC4 with MVC4 web applications before with no problems.
Out of the box it's going to do constructor injection only.
This will work
public HomeController(ITester tester)
{
_tester = tester;
}
This will cause the error you are seeing
public ActionResult Index(ITester tester)
{
return View();
}
So I have the exact opposite problem as MVC5, Web API 2 and Ninject
I have a new MVC5/WebAPI2 project, that has both "Controller"s and "ApiControllers".
I'm using the latest unstable version of Ninject.Web.WebAPI with no code changes to NinjectDependencyResolve.cs and Ninject.WebCommom.cs (besides binding my dependency) the ApiController's constructor injection works. However, when I call a MVC Controller I get:
No parameterless constructor defined for this object.
The issue is you need a Dependency Resolver for both MVC and WebAPI. Depending on which set of Ninject libraries you use, you only get one of those wired in for you.
i.e. if you use the Ninject.Web.WebAPI library you will need to manually set the MVC resolver:
System.Web.Mvc.DependencyResolver.SetResolver(new NinjectResolver(kernel));
(I did this in NinjectWebCommon.cs CreateKernel())
Your Ninject resolver can inherit the interface for both WebAPI and MVC:
public class NinjectResolver : NinjectScope,
System.Web.Http.Dependencies.IDependencyResolver,
System.Web.Mvc.IDependencyResolver
I have MVC3 controllers in external assemblies using MVC contrib's portable areas but I'm having a problem getting Ninject to inject the constructor dependencies.
The controller is found but I get an exception of "No parameterless constructor defined for this object". I can see that ninject's controller factory is being called in the call stack but for external assemblies it just does not pass my database session.
Why could this be?
Probably you should update Ninject. RegisterAllControllersIn is from a version that is outdated since a long time.
You need to register the controllers in the other assemblies using Ninject's RegisterAllControllersIn method.
This is my implementation in the applicationstarted method of global.asax:
foreach (var ass in BuildManager.GetReferencedAssemblies().Cast<Assembly>())
{
RegisterAllControllersIn(ass);
}