Custom Module Initialization

2025/02/11 3:56 PM

I've created a custom module to seed a new application with non-Kentico legacy data. The application is throwing the following error on build while debugging. I am compiling to a package which will be removed after soon after the production release. The error is thrown at the .AddKentico middleware.

Cannot dynamically create an instance of type 'Custom.Admin.SeedModule'. Reason: No parameterless constructor defined. 

   at System.RuntimeType.ActivatorCache..ctor(RuntimeType rt)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
   at CMS.Core.ModuleDiscovery.<>c.<GetModules>b__1_0(RegisterModuleAttribute attribute)
   at System.Linq.Enumerable.SelectArrayIterator`2.MoveNext()
   at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
   at CMS.Core.ModuleDiscovery.GetModules()
   at CMS.Core.ModuleEntryManager.GetDiscoverableModules()
   at CMS.Core.ModuleEntryManager.ModuleCollectionsInitialization()
   at CMS.Core.ModuleEntryManager.PreInit()
   at CMS.Core.AppCore.PreInit()
   at CMS.DataEngine.CMSApplication.PreInit(Boolean initializeContainer)
   at Kentico.Web.Mvc.IServiceCollectionExtensions.PreInitCmsApplication()
   at Kentico.Web.Mvc.IServiceCollectionExtensions.AddKenticoCore(IServiceCollection services)
   at Kentico.Web.Mvc.StartupExtensions.AddKentico(IServiceCollection services, Action`1 featuresBuilder)
   at <CLASS>.Domain.SiteExtensions.ServiceCollections.AddKenticoServices(IServiceCollection services, WebApplicationBuilder builder) in C:\<PATH>\ServiceCollections.cs:line 49
   at Program.<Main>$(String[] args) in C:\<PATH>\Program.cs:line 36

  // ServiceCollection

        services.AddKentico(features =>
        {
            features.UsePageBuilder(new PageBuilderOptions
            {
                //ContentTypeNames = new[]
                //{
                //    ...
                //}
            });
            features.UseActivityTracking();
            features.UseWebPageRouting();
            features.UseEmailStatisticsLogging();
            features.UseEmailMarketing();
        });

//Custom Class Module
  [assembly: AssemblyDiscoverable]
  [assembly: RegisterModule(typeof(SeedModule))]
  namespace Custom.Admin;
  public class SeedModule : Module

//Current Constructor
  public SeedModule(string moduleName) : base(nameof(SeedModule)) {}

//OnInit
  protected override void OnInit()
  {
    base.OnInit();
    Service.Resolve<IEventLogService>().LogInformation("SEED_MODULE", "ORS", "Seed Module initialized");
  }

Environment

Answers

2025/02/11 6:12 PM
Answer

You're close, just replace public SeedModule(string moduleName) with public SeedModule()

You need a parameter less constructor, that has a base that has a name of it.

2025/02/11 8:12 PM

Trevor is correct. Your module class must have a parameterless constructor because you don't ever call var module = new SeedModule("My value"); in your code. Instead, Xperience creates an instance of it through reflection and doesn't know what value to supply for that parameter.

You can see an example in the Kentico Community Portal source code.

2025/02/11 8:17 PM

Thank you, I missed that one little piece.

To answer this question, you have to login first.