Try our conversational search powered by Generative AI!

Getting YSOD with "No parameterless constructor defined" for all controllers after upgrading to CMS 11.x

Vote:
 

I am having YSOD with "No parameterless constructor defined" for all controllers, including the Start page  controller,  after upgrading to CMS 11.x .

Using StructureMap.

Any help or tip would be appreciated.

 

    Server Error in '/' Application. 
 

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. 
 
Source Error:  
 
 

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. 

 
Stack Trace:  
 
 

[MissingMethodException: No parameterless constructor defined for this object.]System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +139 System.Activator.CreateInstance(Type type, Boolean nonPublic) +105System.Activator.CreateInstance(Type type) +12System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +75 [InvalidOperationException: An error occurred when trying to create a controller of type 'DAP.Web.Controllers.Pages.StartPageController'. Make sure that the controller has a parameterless public constructor.]System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +242System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +258System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +77 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +1020System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +195System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +128  

 
 
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3282.0 

#202851
Edited, Apr 02, 2019 16:57
Vote:
 

It is not clear which version you upgraded from, but it sounds like you need to add a dependency resolver like in this link 

https://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/9/Initialization/dependency-injection/

#202857
Apr 02, 2019 23:52
Vote:
 

Thanks for your answer, @Quan Mai.  I already have a dependency resolver (StructureMap) installed from the 10.x version of the application. The software  was upgraded in connection with the migration to 11.x.  In use now is

EpiServer.ServiceLocation.StructureMap 2.0.1
StructureMap 4.6.1
structuremap.web 4.0.0.315

In the 10.x version of the application I am using
structuremap.web-signed 3.1.6.191

structuremap-signed 3.1.9.463

#203035
Apr 08, 2019 9:45
Vote:
 

No the dependency resolver for MVC is different. (Structuremap is a dependency injection (or rather, inversion of control) framework. If you scroll down you would see how to create and register a dependency resolver.

#203038
Apr 08, 2019 11:09
Vote:
 

If I understand you correct Eivind, you had dependency resolver up and running in version 10? If yes, do you see any errors in the log after upgrade?

We have this code in our dependency resolver initializer:

[ModuleDependency(typeof(ServiceContainerInitialization))]
[InitializableModule]
public class DependencyResolverInitialization : IConfigurableModule
{
    public void ConfigureContainer(ServiceConfigurationContext context)
    {
        var container = context.StructureMap();
        container.Configure(ConfigureContainer);

        // This will setup the DependencyResolver with the EPiServer StructureMap Container
        DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
    }

    private static void ConfigureContainer(ConfigurationExpression container)
    {
        container.AddRegistry<StructureMapRegistry>();
    }

    public void Initialize(InitializationEngine context)
    {
    }

    public void Uninitialize(InitializationEngine context)
    {
    }
}

public class StructureMapDependencyResolver : IDependencyResolver
{
    private readonly IContainer _container;
  
    public StructureMapDependencyResolver(IContainer container)
    {
        _container = container;
    }
  
    public object GetService(Type serviceType)
    {
        if (serviceType.IsAbstract || serviceType.IsInterface)
          return _container.TryGetInstance(serviceType);
        return _container.GetInstance(serviceType);
    }
  
    public IEnumerable<object> GetServices(Type serviceType)
    {
        return _container.GetAllInstances(serviceType).Cast<object>();
    }
}
#203047
Edited, Apr 08, 2019 13:38
Vote:
 

Thanks, @Mattias. 

I have almost identical code as you both in my DependencyResolverInitilalization and my StructureMapDependencyResolver.

It leads me to think there are some packages missing in my solution, or my web.config has some faulty or missing entries.
Relevant part of web.config (runtime section):

<dependentAssembly>
<assemblyIdentity name="StructureMap" publicKeyToken="null" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.6.1.0" newVersion="4.6.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="StructureMap.Web" publicKeyToken="null" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="EPiServer.ServiceLocation.StructureMap" publicKeyToken="null" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.1.0" newVersion="2.0.1.0" />
</dependentAssembly>

Unfortunately it is too long since I did the actual upgrade (the process was stopped by the customer), so the logs have been lost.
#203084
Apr 09, 2019 9:47
Vote:
 

Just checked web.config for a project we have and we don't even have assembly redirects for StructureMap. Only for EPiServer.ServiceLocation.StructureMap and that looks identical to yours.

Edit: I lied to you. We do have one redirect but I guess it's irrelevant since we have 4.6.1 installed:

<dependentAssembly>
    <assemblyIdentity name="StructureMap" publicKeyToken="e60ad81abae3c223" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.1.9.463" newVersion="3.1.9.463" />
</dependentAssembly>
#203086
Edited, Apr 09, 2019 10:03
Vote:
 

@Mattias, removing the StructureMap and StructureMap.Web redirects did not help .

It is the call in StructureMapDependencyResolver's method GetConcreteService that fails, in my case using the application's StartPageController as parameter.

I enclose the initial sections of each of those two classes, for comparison .  Both classes are unchanged from my EPi v.10.x application.

using System.Web.Mvc;
using MyApp.Business.Rendering;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;
using EPiServer.Web.Mvc.Html;
using StructureMap;
namespace MyApp.Initialization
{
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class DependencyResolverInitialization : IConfigurableModule

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using StructureMap;
namespace MyApp.Business
{
public class StructureMapDependencyResolver : IDependencyResolver



Could you supply the corresponding (relevant parts of) sections of your code, to see if there is any difference?

I might also try to include a reference to the old signed StructureMap (3.1.9.463) again, like you stiil have, to se what happens.

#203106
Apr 09, 2019 12:54
Vote:
 

This is our usings for resolver:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using StructureMap;

namespace Foundation.Core.Infrastructure
{
    public class StructureMapDependencyResolver : IDependencyResolver

And this is for init module:

using System.Web.Http;
using System.Web.Http.Dispatcher;
using System.Web.Mvc;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;
using Foundation.Core.Infrastructure;
using StructureMap;

namespace Foundation.Web.Initialization
{
    [ModuleDependency(typeof(ServiceContainerInitialization))]
    [InitializableModule]
    public class DependencyResolverInitialization : IConfigurableModule

The main difference I see is the ModuleDependency attribute. We have dependency to ServiceContainerInitialization module instead of EPiServer.Web.InitializationModule.

#203107
Apr 09, 2019 13:12
Vote:
 

Mattias, I changed the ModuleDependency of  DependencyResolverInitialization to be exactly like yours, but the result was not any better.

I feeel I am running out of options, only possibility I see right now is reinserting the old StructureMap ...

#203110
Apr 09, 2019 14:40
Vote:
 

I would suggest to reach out to Developer support service. when we can take a look at your site it would be much easier to find out what is wrong 

#203111
Apr 09, 2019 14:46
Vote:
 

Have you tried adding a breakpoint in your dependency resolver initialization module to see if it's hit when debugging? I remember a co-worker of mine had troubles with initialization modules after an upgrade a while back. He ended up re-installing Nuget packages which surprisingly solved the problem.

#203112
Apr 09, 2019 15:17
Vote:
 

And I just want to make it clear that we do NOT use older StructureMap. We use 4.6.1 too but we had assembly redirects for the older version so we might as well remove it since it has no effect.

#203113
Apr 09, 2019 15:20
Vote:
 

@Quan Mai, the company I work for is not a partner company of EpiServer, and I believe neither is the customer. So we are probably not eligible to developer support?

#203119
Apr 09, 2019 17:32
Vote:
 

Hi, a long shot but it was unclear from the previous information provided. Check your project references and make sure the references don't use the old structuremap versions (also check that your bin folder doesn't anymore have those old structuremap assemblies). Even when you have updated the structuremap NuGet packages your project might still actually reference the old signed structuremap versions and in that case it really doesn't matter what your bindings are having.

#203123
Apr 09, 2019 21:27
Vote:
 

To be honest, I don't know, but you can always reach out for developer support and ask them 

#203127
Apr 09, 2019 22:08
Vote:
 

Hi all who have tried to help.

Sorry to have wasted your time, it turned out to be quite a simple error on my part that caused the problem.

Adding some logging to the StructureMapDependencyResolver class helped me find the culprit.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using EPiServer.Logging;
using StructureMap;

namespace DAP.Web.Business
{
public class StructureMapDependencyResolver : IDependencyResolver
{
private readonly IContainer _container;
private static readonly ILogger Logger = LogManager.GetLogger();

public StructureMapDependencyResolver(IContainer container)
{
_container = container;
}

public object GetService(Type serviceType)
{
if (serviceType.IsInterface || serviceType.IsAbstract)
{
return GetInterfaceService(serviceType);
}
return GetConcreteService(serviceType);
}

private object GetConcreteService(Type serviceType)
{
try
{
// Can't use TryGetInstance here because it won’t create concrete types
return _container.GetInstance(serviceType);
}
catch (StructureMapException e)
{
Logger.Log(Level.Error, $"Error when trying to have StructureMap getting concrete service of type {serviceType}", e);
return null;
}
}

private object GetInterfaceService(Type serviceType)
{
return _container.TryGetInstance(serviceType);
}

public IEnumerable<object> GetServices(Type serviceType)
{
return _container.GetAllInstances(serviceType).Cast<object>();
}
}
}


There was a service project in the solution containing a class that did not respect the  EPi 11 rule concerning ILogger instances.

After that was fixed, the site runs on Epi 11!

#203177
Apr 10, 2019 14:24
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.