First try StructureMap and MVC3 via NuGet

I am trying to understand how to configure StructureMap for ASP.NET MVC3. I already use NuGet and notice that it creates an App_Start folder with a cs file called StructuremapMVC, so I check it and notice that it is the same code, but simplified, which will be written manually in the App_Start section hosted on Global.asax. ..

This is my IoC class code.

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 do I get an exception when I add some IoC on my controllers as the following (I use this template: Entity Framework 4 CTP 4 / CTP 5 General repository template 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(); } 

An exception:

 Exception Details: System.MissingMethodException: No parameterless constructor defined for this object. 
+5
asp.net-mvc asp.net-mvc-3 entity-framework structuremap code-first
Mar 16 2018-11-11T00:
source share
5 answers

You get this error because you do not have the StructureMap setting to resolve the dependencies needed to create the AsambleaController , so it tries to find a constructor without parameters that is not there.

So, you need to install StructureMap for IAsambleaRepository and IUnitOfWork .

On the other hand, I would say that IUnitOfWork should be dependent on your repository, and not on your controller ... your controller does not need to know about the operation device.

+3
Mar 16 '11 at 16:48
source share

To process the parameters in the controller constructor, you need to configure the dependency converter.

Check out the following message on how to connect StructureMap using 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/

+4
Mar 16 2018-11-11T00:
source share

If you followed the publication in the 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>(); 

Call: scan.TheCallingAssembly (); will only look at the MVC project. If you have your services and repositories in another project in your solution, you need to add it as follows:

 scan.Assembly("Your.Assembly"); 
+2
Mar 16 '11 at 16:32
source share

Installing NuGet StructureMap.MVC3 installs the SmDependencyResolver.cs file in the DependencyResolution folder. You will notice that there is a try ... catch in the GetService method that simply returns null if an exception occurs. This can suppress the details of the exception so you can see the error message "no parameterless constructor".

To get more information about the original exception, you can add something to this catch clause to send the original exception information - for example, 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 can help you track down the source of the problem.

+2
Mar 22 '12 at 5:16
source share

Run debugging, you will probably get StructureMap IOC permission error.

Instead of receiving a real permission error, you will receive this message instead. Somewhere along the lines, the MVC pipeline swallows a real mistake.

+1
Apr 05 2018-11-21T00:
source share



All Articles