Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure StructureMap for asp.net MVC 5

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

like image 222
Naveen Avatar asked Oct 20 '13 11:10

Naveen


2 Answers

The following steps worked for me:

  1. Create a new ASP.NET MVC 5 application in Visual Studio 2013 RTM
  2. Install the StructureMap.MVC4 NuGet:

    Install-Package StructureMap.MVC4
    
  3. Create a new interface:

    public interface IDependency
    {
        string SayHello();
    }
    
  4. Implement this interface:

    public class ConcreteDepenedency: IDependency
    {
        public string SayHello()
        {
            return "Hello World";
        }
    }
    
  5. 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());
        }
    }
    
  6. 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;
            }
        }
    }
    
  7. Run your application with Ctrl+F5

  8. The ConcreteDependency is successfully injected in HomeController.
like image 198
Darin Dimitrov Avatar answered Nov 14 '22 14:11

Darin Dimitrov


I have a detailed walk-through showing how to get this working using the latest StructureMap for MVC 5 package here: http://ardalis.com/resolving-dependencies-in-asp.net-mvc-5-with-structuremap

It's not all that different from what was necessary in ASP.NET MVC 3, which I previously published: http://ardalis.com/How-Do-I-Use-StructureMap-with-ASP.NET-MVC-3

Basically you just install the correct NuGet package, modify your controllers to accept dependencies, and wire up your interfaces to your implementations in IoC.cs. If you follow StructureMap's default convention, you can get this wireup to occur automatically for many types.

like image 10
ssmith Avatar answered Nov 14 '22 14:11

ssmith