17

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

Naveen
  • 540
  • 3
  • 7
  • 20
  • It looks like you are attempting to use a StructureMap version which is not compatible with ASP.NET MVC 5. You seem to have attempted to install a StructureMap NuGet that is compiled against an older version of ASP.NET MVC. Which version of the NuGet are you using? – Darin Dimitrov Oct 20 '13 at 11:35
  • Please use https://www.nuget.org/packages/StructureMap.MVC5/ – Paul Aug 22 '14 at 22:35

4 Answers4

40

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.
Kevin
  • 99
  • 1
  • 12
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    This example is working fine but when i'm try to inject a service class im getting below error "Activation error occured while trying to get instance of type HomeController, key """ – Naveen Oct 22 '13 at 08:18
  • 3
    Please use https://www.nuget.org/packages/StructureMap.MVC5/ it is a better impl for MVC5 – Paul Aug 22 '14 at 22:36
  • Thanks buddy. Straight and simple. – hwg Feb 18 '16 at 00:10
10

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.

ssmith
  • 8,092
  • 6
  • 52
  • 93
1

StructureMap (2.6.4.1) is compatible with ASP.NET MVC 5 and you do not have to change anything after the upgrade.

The upgrade process, though, it is not as straightforward as you might think.

Here's a list of all the steps if you have to follow.

PS: I've followed Rick Anderson's tutorial after the upgrade (and he says you should do it before) but it worked, anyway.

Hope it helps.

LeftyX
  • 35,328
  • 21
  • 132
  • 193
  • Darin Dimitrov example is working fine but when im trying to inject service class with repository im getting this error : Activation error occured while trying to get instance of type HomeController, key "" – Naveen Oct 22 '13 at 08:19
-4

Thank you for all the reply finally found the issue. In my service class I refer a app setting but didn't define it on the web.config file because of that structuremap couldn't inject an instance of my service class.

private readonly string _path =
            ConfigurationManager.AppSettings["vp"].ToString(CultureInfo.InvariantCulture);
LeftyX
  • 35,328
  • 21
  • 132
  • 193
Naveen
  • 540
  • 3
  • 7
  • 20
  • 3
    What does the `ToString(CultureInfo.InvariantCulture)` achieve? `AppSettings` is a [`NameValueCollection` which returns `string` for `Item`](http://msdn.microsoft.com/en-us/library/8d0bzeeb(v=vs.110).aspx). `String.ToString(IFormatProvider)` is documented to do ... [nothing](http://msdn.microsoft.com/en-us/library/29dxe1x2(v=vs.110).aspx). – ta.speot.is Dec 17 '13 at 01:44