nopCommerce 4.2 Validation is not working while plugin assembly is not start with "Nop"

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
4 years ago
Hi to all,
Currently I'm working with plugin for nopCommerce 4.20 and my plugin's assembly name is started with custom name instead of "Nop".

I found below code under Nop.Web > Presentation > Nop.Web.Framework > Infrastructure > Extensions > ServiceCollectionExtensions.cs which is inject FluentValidation which is shows that inject only those assembly which is start with "Nop" world.



          
 mvcBuilder.AddFluentValidation(configuration =>
            {
                //register all available validators from Nop assemblies
                var assemblies = mvcBuilder.PartManager.ApplicationParts
                    .OfType<AssemblyPart>()
                    .Where(part => part.Name.StartsWith("Nop", StringComparison.InvariantCultureIgnoreCase))
                    .Select(part => part.Assembly);
                configuration.RegisterValidatorsFromAssemblies(assemblies);

                //implicit/automatic validation of child properties
                configuration.ImplicitlyValidateChildProperties = true;
            });



I tried to add this code snippet in startup file with inherit INopStartup file with my assembly start word, but it not worked.

Any help would be appreciated.
4 years ago
Is this a 3rd party plugin or one of your own?

If it is 3rd party could you not go back to them and say that it doesn't work?

If it is your own plugin (and whilst not ideal) could you not rename the plugin assembly to be Nop.Plugin.[Group].[Name] to work around the issue?

It does sound like an issue though.  If nopcommerce allows you register plugins with any name then it should also register the validation in those plugins.

What is the code that you have added in your own INopStartup that is not working correctly and are you sure that it is getting executed?
4 years ago
davidandrewpowell wrote:
Is this a 3rd party plugin or one of your own?

My own custom plugin

davidandrewpowell wrote:
  
If it is your own plugin (and whilst not ideal) could you not rename the plugin assembly to be Nop.Plugin.[Group].[Name] to work around the issue?

My plugin assembly name like DemoNop.Plugin.Widgets.NivoSlider  (I have tried with change defult NivoSlider slider plugin for testing purpose)

davidandrewpowell wrote:
  
It does sound like an issue though.  If nopcommerce allows you register plugins with any name then it should also register the validation in those plugins.

I don't think nopCommerce 4.20 is suport  with my assembly name.

davidandrewpowell wrote:

What is the code that you have added in your own INopStartup that is not working correctly and are you sure that it is getting executed?


Here is code            
    
//add fluent validation
            services.AddNopMvc().AddFluentValidation(x =>
            {
                //register all available validators from Nop assemblies
                var assemblies = services.AddNopMvc().PartManager.ApplicationParts
                    .OfType<AssemblyPart>()
                    .Where(part => part.Name.StartsWith("DemoNop", StringComparison.InvariantCultureIgnoreCase))
                    .Select(part => part.Assembly);
                x.RegisterValidatorsFromAssemblies(assemblies);

                //implicit/automatic validation of child properties
                x.ImplicitlyValidateChildProperties = true;
            });
    
  

I am getting error during run application:

InvalidOperationException: Sequence contains more than one matching element
System.Linq.Enumerable.SingleOrDefault<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate)

DependencyResolutionException: An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = IObjectModelValidator (DelegateActivator), Services = [Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator], Lifetime = Autofac.Core.Lifetime.RootScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> Sequence contains more than one matching element (See inner exception for details.)
4 years ago
I also followed this post but no luck.
4 years ago
As I understand it, your plugin is named DemoNop.Plugin.Widgets.NivoSlider.

To create this plugin did you take a copy of Nop.Plugin.Widgets.NivoSlider?

If this is the case, when you are running nopcommerce are you running it with both Nop.Plugin.Widgets.NivoSlider and DemoNop.Plugin.Widgets.NivoSlider installed side by side or have you removed the original Nop.Plugin.Widgets.NivoSlider?

I guess I'm trying to establish from the error "Sequence contains more than one matching element" that there are no conflicts from duplicating the NivoSlider plugin.

Also, I still don't understand why you can't instead name the plugin "Nop.Plugin.Widgets.DemoNivoSlider" or similar and avoid the hassle of having to setup your own FluentValidation registration.
4 years ago
any updates on this? i am also facing issue for the validator register at nopcommerce 4.2 for my custom plugin, anyone can help?
4 years ago
Finally I am able to solve this, just add line below to your ConfigureServices where you inherit INopStartup in your custom plugin, it will solve the issue:

    //register validator
    services.AddTransient<IValidator<CustomRegisterModel>, RegisterValidator>();



overall code will be as below:

/// <summary>
        /// Add and configure any of the middleware
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        /// <param name="configuration">Configuration root of the application</param>
        //public void ConfigureServices(IServiceCollection services, IConfigurationRoot configuration)
        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
          

            //register validator
            services.AddTransient<IValidator<CustomRegisterModel>, RegisterValidator>();


        }
4 years ago
ysheah wrote:
any updates on this? i am also facing issue for the validator register at nopcommerce 4.2 for my custom plugin, anyone can help?


Are you also using a plugin name that does not follow nopcommerce convention? What name did you use for your plugin?

Can you provide any more information as to what you have done for someone to potentially recreate this issue?
4 years ago
davidandrewpowell wrote:
any updates on this? i am also facing issue for the validator register at nopcommerce 4.2 for my custom plugin, anyone can help?

Are you also using a plugin name that does not follow nopcommerce convention? What name did you use for your plugin?

Can you provide any more information as to what you have done for someone to potentially recreate this issue?



Hi, davidandrewpowell, yes my plugin name is custom name that not following the nopcommerce standard which is Nop.Plugin.Misc... My Plugin name is like mycompanyname.plugin.misc.name. In nopcommerce 4.2, I believe we will need to register the validator ourself at our custom name plugin, the solution I have stated at previous reply.
4 years ago
ysheah wrote:
Hi, davidandrewpowell, yes my plugin name is custom name that not following the nopcommerce standard which is Nop.Plugin.Misc... My Plugin name is like mycompanyname.plugin.misc.name. In nopcommerce 4.2, I believe we will need to register the validator ourself at our custom name plugin, the solution I have stated at previous reply.


I must have just missed your response with your solution. Glad you managed to get it working.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.