First try StructureMap and MVC3 via NuGet
Asked Answered
R

5

5

I'm trying to figure how to config StructureMap for ASP.NET MVC3 I've already using NuGet and I notice that it creates App_Start Folder with a cs file named as StructuremapMVC, so I check it and notice that is the same code but simplified that will be written manually on App_Start section placed on Global.asax...

This is my code from IoC Class

public static class IoC
    {
        public static IContainer Initialize()
        {
            ObjectFactory.Initialize(x =>
                        {
                            x.Scan(scan =>
                                    {
                                        scan.TheCallingAssembly();
                                        scan.WithDefaultConventions();
                                        scan.AddAllTypesOf<IController>();
                                    });
                            x.For<OpcionDB>().Use(() => new DatabaseFactory().Get());
                        });
            return ObjectFactory.Container;
        }
    }

My Question is Why I get an Exception when I inject some IoC on my Controllers as the follow (I use this pattern : Entity Framework 4 CTP 4 / CTP 5 Generic Repository Pattern and Unit Testable) :

        private readonly IAsambleaRepository _aRep;
        private readonly IUnitOfWork _uOw;

        public AsambleaController(IAsambleaRepository aRep, IUnitOfWork uOw)
        {
            _aRep = aRep;
            this._uOw = uOw;
        }

        public ActionResult List(string period)
        {
            var rs = _aRep.ByPeriodo(period).ToList<Asamblea>();

            return View();
        }

Exception showed :

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.
Ramiroramjet answered 16/3, 2011 at 0:8 Comment(0)
U
3

You are getting that error because you haven't setup StructureMap to resolve the dependencies needed to contruct the AsambleaController so it tries to find a parameterless constructor which there isn't one.

So what you need to do is setup StructureMap for IAsambleaRepository and IUnitOfWork.

On a side note, I'd say that IUnitOfWork should be a dependency on your repository and not your controller... your controller shouldn't need to know about the unit of work.

Ushaushant answered 16/3, 2011 at 16:48 Comment(0)
S
4

To handle parameters in the controller's constructor, the dependency resolver must be configured.

Check the following post on how to wire up the StructureMap with ASP.NET MVC3:

http://stevesmithblog.com/blog/how-do-i-use-structuremap-with-asp-net-mvc-3/

http://codebetter.com/jeremymiller/2011/01/23/if-you-are-using-structuremap-with-mvc3-please-read-this/

Selffertilization answered 16/3, 2011 at 4:54 Comment(0)
U
3

You are getting that error because you haven't setup StructureMap to resolve the dependencies needed to contruct the AsambleaController so it tries to find a parameterless constructor which there isn't one.

So what you need to do is setup StructureMap for IAsambleaRepository and IUnitOfWork.

On a side note, I'd say that IUnitOfWork should be a dependency on your repository and not your controller... your controller shouldn't need to know about the unit of work.

Ushaushant answered 16/3, 2011 at 16:48 Comment(0)
A
2

If you followed the post on Repository you will want to add these configurations to your IoC.cs File:

x.For<IUnitOfWork>().HttpContextScoped().Use<UnitOfWork>();
x.For<IDatabaseFacroey>().HttpContextScoped().Use<DatabaseFactory>();
x.For<IAsambleaRepository >().HttpContextScoped().Use<AsambleaRepository>();

The call to: scan.TheCallingAssembly(); will only look at the MVC project. If you have your services and repositories in a different project in your solution you will need to add it like this:

scan.Assembly("Your.Assembly");
Assuntaassur answered 16/3, 2011 at 16:32 Comment(0)
A
2

The NuGet installation of StructureMap.MVC3 installs a file called SmDependencyResolver.cs in the folder DependencyResolution. You'll notice that the GetService method in there has a try...catch that just returns null if an exception occurs. This can suppress the details of an exception so that you end up seeing the error message about "no parameterless constructor" instead.

To get more information about the original exception you can add something in that catch clause to spit out the original exception's details - for example the Debug.WriteLine here:

    public object GetService(Type serviceType)
    {
        if (serviceType == null) return null;
        try
        {
            return serviceType.IsAbstract || serviceType.IsInterface
                     ? _container.TryGetInstance(serviceType)
                     : _container.GetInstance(serviceType);
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.ToString());
            return null;
        }
    }

This may help you track down the source of the problem.

Anstice answered 22/3, 2012 at 5:16 Comment(1)
In all my years of using StructureMap I never noticed this, i think i'm going to rip this try block out. I don't think I ever want StructureMap to not bubble up.Iatrochemistry
I
1

Run in debug, you're probably getting a StructureMap IOC resolution error.

Instead of getting the real resolution error you get displayed this message instead. Somewhere along the lines the MVC pipeline swallows the real error.

Iatrochemistry answered 5/4, 2011 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.