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 Gurus,

I'm new in creating plugin in NopCommerce and tried to download and install this plugin found in this site.
https://www.nopcommerce.com/docs/75/plugin-with-data-access.aspx

The issue is that when I hit the url '/tracking/productviews/1' it displays this error:


[MissingMethodException: No parameterless constructor defined for this object.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +69
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +67

[InvalidOperationException: An error occurred when trying to create a controller of type 'Nop.Plugin.Other.ProductViewTracker.Controllers.TrackingController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +182
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +199
   System.Web.Mvc.<>c__DisplayClass6.<BeginProcessRequest>b__2() +49
   System.Web.Mvc.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() +13
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +124
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +98
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8862676
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184



I've tried to copy and run the code in this link as well:
http://blog.csharpwebdeveloper.com/2011/09/10/writing-a-plugin-for-nopcommerce-2-x/

But I'm having the same issue. Tried to clean and build the codes but same issue.

Tried to make my own simple controller by following the link above:
private readonly IProductService _productService;


        public TestPluginController(IProductService productService)
        {
            this._productService = productService;
        }


but is seems that IProductService is not registered in my created controllers. Am I missing some codes.

Hope you can help me out.

Regards,

Allan
11 years ago
Please post your DependencyRegistrar class.
11 years ago
hi Andy,

This is the DependencyRegistrar class for the example in this link:
public class ProductViewTrackerDependencyRegistrar : IDependencyRegistrar
    {
        private const string CONTEXT_NAME = "nop_object_context_product_view_tracker";

        #region Implementation of IDependencyRegistrar

        public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            //Load custom data settings
            var dataSettingsManager = new DataSettingsManager();
            DataSettings dataSettings = dataSettingsManager.LoadSettings();

            //Register custom object context
            builder.Register<IDbContext>(c => RegisterIDbContext(c, dataSettings)).Named<IDbContext>(CONTEXT_NAME).InstancePerHttpRequest();
            builder.Register(c => RegisterIDbContext(c, dataSettings)).InstancePerHttpRequest();

            //Register services
            builder.RegisterType<ViewTrackingService>().As<IViewTrackingService>().InstancePerHttpRequest();

            //Override the repository injection
            builder.RegisterType<EfRepository<TrackingRecord>>().As<IRepository<TrackingRecord>>().WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME)).InstancePerHttpRequest();
        }

        #endregion

        #region Implementation of IDependencyRegistrar

        public int Order
        {
            get { return 0; }
        }

        #endregion

        /// <summary>
        /// Registers the I db context.
        /// </summary>
        /// <param name="componentContext">The component context.</param>
        /// <param name="dataSettings">The data settings.</param>
        /// <returns></returns>
        private TrackingRecordObjectContext RegisterIDbContext(IComponentContext componentContext, DataSettings dataSettings)
        {
            string dataConnectionStrings;

            if (dataSettings != null && dataSettings.IsValid())
            {
                dataConnectionStrings = dataSettings.DataConnectionString;
            }
            else
            {
                dataConnectionStrings = componentContext.Resolve<DataSettings>().DataConnectionString;
            }

            return new TrackingRecordObjectContext(dataConnectionStrings);
        }
    }


And for the other one ([url" rel="nofollow,ugc">http://blog.csharpwebdeveloper.com/2011/09/10/writing-a-plugin-for-nopcommerce-2-x/
) there is no DependencyRegistrar class.

Is it necessary to have DependencyRegistrar class in my plugin even if the IProductService is already registered in Nop.Web.Framework.DependencyRegistrar class?

Regards,

Allan
11 years ago
hi Andy,

This is the DependencyRegistrar class for the example in this link: https://www.nopcommerce.com/docs/75/plugin-with-data-access.aspx

public class ProductViewTrackerDependencyRegistrar : IDependencyRegistrar
    {
        private const string CONTEXT_NAME = "nop_object_context_product_view_tracker";

        #region Implementation of IDependencyRegistrar

        public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            //Load custom data settings
            var dataSettingsManager = new DataSettingsManager();
            DataSettings dataSettings = dataSettingsManager.LoadSettings();

            //Register custom object context
            builder.Register<IDbContext>(c => RegisterIDbContext(c, dataSettings)).Named<IDbContext>(CONTEXT_NAME).InstancePerHttpRequest();
            builder.Register(c => RegisterIDbContext(c, dataSettings)).InstancePerHttpRequest();

            //Register services
            builder.RegisterType<ViewTrackingService>().As<IViewTrackingService>().InstancePerHttpRequest();

            //Override the repository injection
            builder.RegisterType<EfRepository<TrackingRecord>>().As<IRepository<TrackingRecord>>().WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME)).InstancePerHttpRequest();
        }

        #endregion

        #region Implementation of IDependencyRegistrar

        public int Order
        {
            get { return 0; }
        }

        #endregion

        /// <summary>
        /// Registers the I db context.
        /// </summary>
        /// <param name="componentContext">The component context.</param>
        /// <param name="dataSettings">The data settings.</param>
        /// <returns></returns>
        private TrackingRecordObjectContext RegisterIDbContext(IComponentContext componentContext, DataSettings dataSettings)
        {
            string dataConnectionStrings;

            if (dataSettings != null && dataSettings.IsValid())
            {
                dataConnectionStrings = dataSettings.DataConnectionString;
            }
            else
            {
                dataConnectionStrings = componentContext.Resolve<DataSettings>().DataConnectionString;
            }

            return new TrackingRecordObjectContext(dataConnectionStrings);
        }
    }


And for the other one (http://blog.csharpwebdeveloper.com/2011/09/10/writing-a-plugin-for-nopcommerce-2-x/) there is no DependencyRegistrar class.

Is it necessary to have DependencyRegistrar class in my plugin even if the IProductService is already registered in Nop.Web.Framework.DependencyRegistrar class?

Regards,

Allan
11 years ago
Yes, you do need a DependencyRegistrar class in your plugin.  It's not created to do anything with IProductService, it is to register the services and repositories that created are in your plugin.

The code looks ok from what I can see, do you have a reference to Nop.Services in the plugin project?
11 years ago
Hi Andy,

Thanks for the reply.

Have you tried running the sample from this link?
https://www.nopcommerce.com/docs/75/plugin-with-data-access.aspx

The code is complete (I think), I downloaded it, changed the version to 2.60, compiled and installed in NopCommerce but it gives me the error when running it.

Weird is that I tried to duplicate the Nop.Plugin.Tax.StrikeIron plugin just changed class name etc. to make it StrikeIronTest and when I run it, it still gives me the same error.


Regards,

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