I'm getting below error. I setup it similar to asp.net mvc 4.
No parameterless constructor defined for this object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.
Finally found the actual exception "Activation error occured while trying to get instance of type HomeController, key """
Error is occuring when i'm going to inject service class to the home contoller
The following steps worked for me:
Install the StructureMap.MVC4
NuGet:
Install-Package StructureMap.MVC4
Create a new interface:
public interface IDependency
{
string SayHello();
}
Implement this interface:
public class ConcreteDepenedency: IDependency
{
public string SayHello()
{
return "Hello World";
}
}
Have the HomeController work with this interface:
public class HomeController : Controller
{
private readonly IDependency dependency;
public HomeController(IDependency dependency)
{
this.dependency = dependency;
}
public ActionResult Index()
{
return Content(this.dependency.SayHello());
}
}
Configure your container in ~/DependencyResolution/Ioc.cs
:
using StructureMap;
using WebApplication1.Controllers;
namespace WebApplication1.DependencyResolution {
public static class IoC {
public static IContainer Initialize() {
ObjectFactory.Initialize(x =>
{
x.For<IDependency>().Use<ConcreteDepenedency>();
});
return ObjectFactory.Container;
}
}
}
Run your application with Ctrl+F5
ConcreteDependency
is successfully injected in HomeController
.