Try our conversational search powered by Generative AI!

System.InvalidOperationException: An error occurred when trying to create a controller of type 'AVG.Web.Controllers.Pages.StandardPageControlle

Vote:
 

Hi,

I have been implemented Episerver Insight like bellow.

It very strange, I don't know why i can't get instance of ICmsTrackingService even i already registered it.

Can anybody show me clue?

System.InvalidOperationException: An error occurred when trying to create a controller of type 'AVG.Web.Controllers.Pages.StandardPageController'. Make sure that the controller has a parameterless public constructor. ---> System.MissingMethodException: No parameterless constructor defined for this object.
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
--- End of inner exception stack trace ---
at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.<>c__DisplayClass285_0.<ExecuteStepImpl>b__0()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
System.InvalidOperationException: An error occurred when trying to create a controller of type 'AVG.Web.Controllers.Pages.StandardPageController'. Make sure that the controller has a parameterless public constructor. ---> System.MissingMethodException: No parameterless constructor defined for this object.
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
--- End of inner exception stack trace ---
at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.<>c__DisplayClass285_0.<ExecuteStepImpl>b__0()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
2020-08-13 14:48:27,359 [107] ERROR AVG.Web.EPiServerApplication: No parameterless constructor defined for this object.

 public interface ICmsTrackingService
    {
        Task TrackPage(PageData pageData, HttpContextBase httpContextBase);
    }

public class CmsTrackingService : ICmsTrackingService
    {
        private readonly ITrackingService _trackingService;
        private readonly IContextModeResolver _contextModeResolver;
        private readonly ISiteDefinitionResolver _siteDefinitionResolver;
        private readonly IContentLoader _contentLoader;
        private readonly ILogger _logger = LogManager.GetLogger(typeof(CmsTrackingService));

        public CmsTrackingService(
            ITrackingService trackingService,
            IContextModeResolver contextModeResolver,
            ISiteDefinitionResolver siteDefinitionResolver,
            IContentLoader contentLoader) {
            _trackingService = trackingService;
            _contextModeResolver = contextModeResolver;
            _siteDefinitionResolver = siteDefinitionResolver;
            _contentLoader = contentLoader;
        }
public virtual async Task TrackPage(PageData pageData, HttpContextBase httpContextBase){
...
}
}

context.Services.AddTransient<ICmsTrackingService, CmsTrackingService>();


  public class StandardPageController : PageControllerBase<StandardPage>
    {

        private readonly ICmsTrackingService _cmsTrackingService;
        public StandardPageController(ICmsTrackingService cmsTrackingService)
        {
            _cmsTrackingService = cmsTrackingService;
        }
}
}


#226454
Edited, Aug 13, 2020 11:11
Vote:
 

You need a DependencyResolver to let asp.net mvc knows how to create controller instances from parameterized constructors. Then register it, here is an example 

https://github.com/episerver/Quicksilver/blob/124daf46b04cc9fea4f1e423e09ef53202c1770f/Sources/EPiServer.Reference.Commerce.Site/Infrastructure/SiteInitialization.cs#L100

#226462
Aug 13, 2020 13:05
Vote:
 

Hi Quan,

I did it already, i have to install all packeges on .web project and it work now.

Thanks

Thang Le

#226512
Aug 14, 2020 6:38
Vote:
 

Hi Thange,

I think the problem in the Constructor because error says 'Make sure that the controller has a parameterless public constructor. ' So try below suggestions:

  • Make sure the PageControllerBase constructor is public.
  • Check the PageControllerBase constructor, if the PageControllerBase is parameterized constructor then call parent constructor from StandardPageController using :base([Pass the dependencies]) like below:
public class StandardPageController : PageControllerBase<StandardPage>
{

        private readonly ICmsTrackingService _cmsTrackingService;
        public StandardPageController(ICmsTrackingService cmsTrackingService)
        :base([Pass the dependencies])
        {
            _cmsTrackingService = cmsTrackingService;
        }
}
#226770
Aug 20, 2020 3:42
* 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.