locked
No parameterless constructor defined for this object RRS feed

  • Question

  • User-303331888 posted

    Am getting "No parameterless constructor defined for this object" in the return statement of below line of code. also i have added definition of the interface. I am not sure why am getting this error. Could you please help how we can resolve this error?

    private T getPageById<T>(Guid id, IApi p_api) where T : PageBase, new()
            {
                IApi api = p_api;
                if (api == null)
                    api = m_getScopedServices.GetCurrentApi();
                 return api.Pages.GetById<T>((Guid)id) as T;
            }

    IApi interface:

    public interface IApi : IDisposable
        {
            IAliasRepository Aliases { get; }
            IArchiveRepository Archives { get; }
            ICategoryRepository Categories { get; }
            IMediaRepository Media { get; }
            IPageRepository Pages { get; }
            IPageTypeRepository PageTypes { get; }
            IParamRepository Params { get; }
            IPostRepository Posts { get; }
            IPostTypeRepository PostTypes { get; }
            ISiteRepository Sites { get; }
            ISiteTypeRepository SiteTypes { get; }
            ITagRepository Tags { get; }
        }

    Monday, September 28, 2020 11:42 AM

All replies

  • User475983607 posted

    The error is very clear.  Add a parameterless constructor to the class to the service (class) causing the error.

    Monday, September 28, 2020 12:03 PM
  • User1120430333 posted

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors#:~:text=A%20constructor%20that%20takes%20no,more%20information%2C%20see%20Instance%20Constructors.

    <copied>

    A constructor that takes no parameters is called a parameterless constructor. Parameterless constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new.

    <end>

    An IoC instantiates an object too. You will get the error. 

    Monday, September 28, 2020 3:14 PM
  • User-474980206 posted

    if it is the return line, then either the Pages property or the GetById method have a bug where they explicitly or implicitly try to dynamically create an object instance which requires a parameter.

    Monday, September 28, 2020 3:30 PM
  • User-303331888 posted

    How can we check whether it is creating object instance dynamically?

    Tuesday, September 29, 2020 7:58 AM
  • User753101303 posted

    Hi,

    Don't you have the code for GetById? The full exception message should also include the location where it happens.

    Tuesday, September 29, 2020 8:16 AM
  • User-303331888 posted

    Below is the exception details. Could you please help me where is the exact issue?

    System.MissingMethodException
      HResult=0x80131513
      Message=No parameterless constructor defined for this object.
      Source=System.Private.CoreLib
      StackTrace:
       at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor)
       at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean skipCheckThis, Boolean fillCache)
       at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
       at Piranha.Services.ContentService`3.CreateDynamicRegion(IServiceScope scope, RegionType region)
       at Piranha.Services.ContentService`3.Create[T](ContentType contentType)
       at Piranha.Services.ContentService`3.Transform[T](TContent content, ContentType type, Action`2 process)
       at Piranha.Repositories.PageRepository.GetById[T](Guid id)
       at BetD.MashupQS.Website.Ui.Helpers.PageHelper.getPageById[T](Guid id, IApi p_api) in C:\Users\E0433988\Desktop\CMD_Mashup\Deborah's issue\Source_code\BetD.MashupQS.Web.UI\Helpers\PageHelper.cs:line 376
       at BetD.MashupQS.Website.Ui.Helpers.PageHelper.GetElement[T](Guid id, Func`3 getFunction) in C:\Users\E0433988\Desktop\CMD_Mashup\Deborah's issue\Source_code\BetD.MashupQS.Web.UI\Helpers\PageHelper.cs:line 77
       at BetD.MashupQS.Website.Ui.Helpers.PageHelper.GetById[T](Guid p_PageId, IApi p_api) in C:\Users\E0433988\Desktop\CMD_Mashup\Deborah's issue\Source_code\BetD.MashupQS.Web.UI\Helpers\PageHelper.cs:line 368
       at BetD.MashupQS.Website.Ui.Helpers.PageHelper.GetById(Guid p_PageId, IApi p_api) in C:\Users\E0433988\Desktop\CMD_Mashup\Deborah's issue\Source_code\BetD.MashupQS.Web.UI\Helpers\PageHelper.cs:line 519
       at BetD.MashupQS.Website.Ui.Helpers.PageHelper.GetPermalink(Guid p_PageId) in C:\Users\E0433988\Desktop\CMD_Mashup\Deborah's issue\Source_code\BetD.MashupQS.Web.UI\Helpers\PageHelper.cs:line 407
       at BetD.MashupQS.Website.Ui.Analytics.AnalyticsLogFactory.LogEntry(Guid p_id) in C:\Users\E0433988\Desktop\CMD_Mashup\Deborah's issue\Source_code\BetD.MashupQS.Web.UI\Analytics\AnalyticsLogFactory.cs:line 70
       at BetD.MashupQS.Website.Ui.Controllers.CmsController.QlikWheel(Guid id) in C:\Users\E0433988\Desktop\CMD_Mashup\Deborah's issue\Source_code\BetD.MashupQS.Web.UI\Controllers\CmsController.cs:line 98
       at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
       at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()

    Wednesday, September 30, 2020 7:22 AM
  • User-474980206 posted

    The error message stack trace

    Piranha.Repositories.PageRepository.GetById[T](Guid id)

    clearly show that this method has a bug in it. It probably is calling for an object from services that can not be created. As you show none of the code we can not help you. What was wrong with using the debugger to find such a simple coding error?

    Wednesday, September 30, 2020 6:09 PM
  • User753101303 posted

    So it seems you are using https://piranhacms.org/docs/manager-architecture/introduction and have a problem with dependency injection configuration (either a class which should have a constructor with no parameters or maybe you should register as well its dependencies so that DI can handle its creation)..

    Thursday, October 1, 2020 7:15 AM