Note that there are some explanatory texts on larger screens.

plurals
  1. POStructuremap does not work on MVC4
    primarykey
    data
    text
    <p>I've used <code>StructureMap</code> in MVC2/3 many times without any concern, but I guess handling IoC is different in MVC4. When i used <code>StructureMap</code> for handling IoC in MVC4 I get the following exception.:</p> <h2>No parameterless constructor defined for this object</h2> <p>Why? I have not found any correct result in google except this: <a href="http://connect.microsoft.com/VisualStudio/feedback/details/725313/ioc-not-working-in-mvc4" rel="noreferrer">IoC Not Working In MVC4</a> These is my IoC classes:</p> <pre><code>public static class IoC { public static IContainer Initialize() { ObjectFactory.Initialize(x =&gt; { x.Scan(scan =&gt; { //scan.Assembly("DLL.Core"); scan.Assembly("DLL.CMS"); scan.TheCallingAssembly(); scan.WithDefaultConventions(); }); x.For&lt;IDbContext&gt;().Use&lt;ModelEntities&gt;(); x.For(typeof(IRepository&lt;&gt;)).Use(typeof(Repository&lt;&gt;)); x.For&lt;IHttpControllerActivator&gt;(); x.For&lt;IController&gt;(); }); return ObjectFactory.Container; } </code></pre> <p>And SmDependencyResolver is:</p> <pre><code> public class SmDependencyResolver : IDependencyResolver { private readonly IContainer _container; public SmDependencyResolver(IContainer container) { _container = container; } public object GetService(Type serviceType) { if (serviceType == null) return null; try { return serviceType.IsAbstract || serviceType.IsInterface ? _container.TryGetInstance(serviceType) : _container.GetInstance(serviceType); } catch { return null; } } public IEnumerable&lt;object&gt; GetServices(Type serviceType) { return _container.GetAllInstances(serviceType).Cast&lt;object&gt;(); } } </code></pre> <p>And my error is:</p> <blockquote> <p>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. </p> <pre><code>Exception Details: System.MissingMethodException: No parameterless constructor defined for this object. </code></pre> <p>Source Error: </p> <p>An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</p> <p>Stack Trace: </p> <p>[MissingMethodException: No parameterless constructor defined for this object.] System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean&amp; canBeCached, RuntimeMethodHandleInternal&amp; ctor, Boolean&amp; bNeedSecurityCheck) +0<br> System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98<br> System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241 System.Activator.CreateInstance(Type type, Boolean nonPublic) +69<br> System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +67</p> <p>[InvalidOperationException: An error occurred when trying to create a controller of type 'Parsian.Web.Areas.Dashboard.Controllers.MemberController'. Make sure that the controller has a parameterless public constructor.]<br> System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +182<br> System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80<br> System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74<br> System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController&amp; controller, IControllerFactory&amp; factory) +196 System.Web.Mvc.&lt;>c__DisplayClass6.b__2() +49 System.Web.Mvc.&lt;>c__DisplayClassb<code>1.&lt;ProcessInApplicationTrust&gt;b__a() +13 System.Web.Mvc.SecurityUtil.&lt;GetCallInAppTrustThunk&gt;b__0(Action f) +7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22<br> System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func</code>1 func) +124 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +98<br> System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50<br> System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16<br> System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8862676 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +184</p> </blockquote> <p>Thanks for correct answers. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload