No parameterless constructor defined for this object.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Hi,

Here is my error:

[InvalidOperationException: An error occurred when trying to create a controller of type 'Nop.Plugin.Misc.LicenseKeyGenerator.Controllers.LicenseKeyGeneratorController'. Make sure that the controller has a parameterless public constructor.]


I have registered my custom type here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nop.Core.Infrastructure.DependencyManagement;
using Nop.Core.Data;
using Autofac;
using Nop.Core;
using Nop.Data;

namespace Nop.Plugin.Misc.LicenseKeyGenerator
{
    public class LicenseKeyDependencyRegistrar : IDependencyRegistrar
    {

        public int Order
        {
            get { return 0; }
        }

        public void Register(Autofac.ContainerBuilder builder, Core.Infrastructure.ITypeFinder typeFinder)
        {
            builder.RegisterType<LicenseKeyProcessor>().As<ILicenseKeyProcessor>();
        }
    }
}


ILicenseKeyProcessor has no methods. Here is my code:

namespace Nop.Plugin.Misc.LicenseKeyGenerator
{
    public class LicenseKeyProcessor : BasePlugin, IAdminMenuPlugin, ILicenseKeyProcessor
    {
        ISettingService _settingService;
        
        public LicenseKeyProcessor(ISettingService settingService)
        {
            _settingService = settingService;
        }

        public override void Install()
        {
            var settings = new LicenseKeyGeneratorSettings()
            {
                OrderIDQueryString = "OrderId"
            };

            _settingService.SaveSetting(settings);

            base.Install();
        }

        public void BuildMenuItem(Telerik.Web.Mvc.UI.MenuItemBuilder menuItemBuilder)
        {
            menuItemBuilder.Text("License Key Generator");
            menuItemBuilder.Url("/Admin/Plugin/LicenseKeyGenerator/Admin/Configure");
            menuItemBuilder.Route("Admin.Plugin.LicenseKeyGenerator.Configure");

        }

        public string MakeLicenseKey()
        {
            return "abcf";
        }
    }
}


namespace Nop.Plugin.Misc.LicenseKeyGenerator.Controllers
{
    [AdminAuthorize]
    public class LicenseKeyGeneratorController : BaseNopController
    {
        LicenseKeyGeneratorSettings licenseKeyGeneratorSettings;
        ISettingService settingService;
        ILicenseKeyProcessor licenseKeyProcessor;

        public LicenseKeyGeneratorController(LicenseKeyGeneratorSettings licenseKeyGeneratorSettings, SettingService settingService, ILicenseKeyProcessor licenseKeyProcessor)
        {
            this.licenseKeyGeneratorSettings = licenseKeyGeneratorSettings;
            this.settingService = settingService;
            this.licenseKeyProcessor = licenseKeyProcessor;
        }

        [AdminAuthorize]
        public ActionResult Configure()
        {
            var model = new LicenseKeyGeneratorModel();
            model.OrderIDQueryString = licenseKeyGeneratorSettings.OrderIDQueryString;
            return View("Nop.Plugin.Misc.LicenseKeyGenerator.Views.LicenseKeyGenerator.Configure", model);
        }

        /// <summary>
        /// saves settings to nopCommerce in admin
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, ActionName("Configure")]
        [FormValueRequired("save")]
        public ActionResult ConfigurePOST(LicenseKeyGeneratorModel model)
        {
            if (!ModelState.IsValid)
            {
                return Configure();
            }

            licenseKeyGeneratorSettings.OrderIDQueryString = model.OrderIDQueryString;
            settingService.SaveSetting(licenseKeyGeneratorSettings);

            return View("Nop.Plugin.Misc.LicenseKeyGenerator.Views.LicenseKeyGenerator.Configure", model);
        }

    }
11 years ago
The other dependencies in the core code have .InstancePerHttpRequest(); on the end of the RegisterType function.  Try that.  

Does LicenseKeyGeneratorSettings implement ISettings?
11 years ago
Hi,

Found out the issue. You must use ISettingService:


        public LicenseKeyGeneratorController(LicenseKeyGeneratorSettings licenseKeyGeneratorSettings, ISettingService settingService)
        {
            this.licenseKeyGeneratorSettings = licenseKeyGeneratorSettings;
            this.settingService = settingService;
            //this.licenseKeyProcessor = licenseKeyProcessor;
        }

I used SettingService.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.