Uninstalled plugin still being used

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
9 năm cách đây
One thing I am thinking is, DependencyRegistrar gets called only on App Starts. That means it's called once per application pool recycle. And as far as I can see, uninstalling plugins doesn't restart apps, which means it won't work like how you want it to even if you write it inside DependencyRegistrar. Unless I am missing anything, I think writing inside the method is needed.

So your method will check if plugin is installed: if yes, run code; if not, call the original nopCommerce interface / method.

:P
8 năm cách đây
Try this in DependencyRegistrar


var pluginFinder= new PluginFinder();
pluginFinder.ReloadPlugins();
        
var pluginDescriptor = pluginFinder.GetPluginDescriptorBySystemName("Nop.MyPlugin");


  if (pluginDescriptor!=null)
  {
  builder.RegisterType<MyPluginService>().As<IPriceCalculationService>().InstancePerRequest();

  }




hope this makes it easier.
6 năm cách đây
Not sure if this is still relevant, but here is a sollution:


You can use this in the Register method to check if the plugin is installed:



            // Is this plugin installed??
            var isInstalled = false;
            var types = typeFinder.FindClassesOfType<IPluginFinder>(true);
            if (types.Count() == 1)
            {
                var plugins = Activator.CreateInstance(types.First()) as IPluginFinder;
                var plug = plugins.GetPluginDescriptorBySystemName("Miscellaneous.plugin");

                if (plug != null && plug.Installed)
                {
                    isInstalled = true;
                }
            }

5 năm cách đây
henksalomons wrote:
Not sure if this is still relevant, but here is a sollution:


You can use this in the Register method to check if the plugin is installed:



            // Is this plugin installed??
            var isInstalled = false;
            var types = typeFinder.FindClassesOfType<IPluginFinder>(true);
            if (types.Count() == 1)
            {
                var plugins = Activator.CreateInstance(types.First()) as IPluginFinder;
                var plug = plugins.GetPluginDescriptorBySystemName("Miscellaneous.plugin");

                if (plug != null && plug.Installed)
                {
                    isInstalled = true;
                }
            }



I elaborated a bit on this to fit my needs, others may find it universally helpful.


        private static bool IsPluginInstalled(ITypeFinder typeFinder)
        {
            var isInstalled = false;
            var types = typeFinder.FindClassesOfType<IPluginFinder>(true).ToList();
            if (types.Count() != 1) return false;
            var plugins = Activator.CreateInstance(types.First()) as IPluginFinder;
            var thisPlugin = GetPluginInfo();
            if (thisPlugin == null) return false;
            var plug = plugins?.GetPluginDescriptorBySystemName(thisPlugin.SystemName);
            if (plug != null && plug.Installed)
                isInstalled = true;

            return isInstalled;
        }

        private static PluginDescriptor GetPluginInfo()
        {
            try
            {
                var pluginFullName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
                var pluginShortName = pluginFullName.Replace("Nop.Plugin.", string.Empty);
                var json = File.ReadAllText($"Plugins/{pluginShortName}/plugin.json");
                var descr = JsonConvert.DeserializeObject<PluginDescriptor>(json);
                return descr;
            }
            catch (Exception e)
            {
                if (Debugger.IsAttached)
                    Console.WriteLine(e);
                throw;
            }
        }


Then in my DependencyRegistrar's Register function:

var installed = IsPluginInstalled(typeFinder);
if (!installed) return;
...
5 năm cách đây
henksalomons wrote:
Not sure if this is still relevant, but here is a sollution:


You can use this in the Register method to check if the plugin is installed:



            // Is this plugin installed??
            var isInstalled = false;
            var types = typeFinder.FindClassesOfType<IPluginFinder>(true);
            if (types.Count() == 1)
            {
                var plugins = Activator.CreateInstance(types.First()) as IPluginFinder;
                var plug = plugins.GetPluginDescriptorBySystemName("Miscellaneous.plugin");

                if (plug != null && plug.Installed)
                {
                    isInstalled = true;
                }
            }



Hi everybody.
Unfortunately, this code can't work in nop version 4.10,because we must add one instanse of IEventpublisher to the Activator.CreateInstance,as below:

   var plugins = Activator.CreateInstance(types.First(), eventPublisher) as IPluginFinder;


But the point is, how to initialize event publisher in this section!?

Thanks.
5 năm cách đây
This method of answer can give:


            var subscriptionService = new SubscriptionService();
            var eventPublisher = new EventPublisher(subscriptionService);
            var types = typeFinder.FindClassesOfType<IPluginFinder>();

            var enumerable = types as Type[] ?? types.ToArray();
            if (enumerable.Count() == 1)
            {
                if (Activator.CreateInstance(enumerable.First(), eventPublisher) is IPluginFinder pluginFinder)
                {
                    var currentPlugin = pluginFinder.GetPluginDescriptorBySystemName("misc.myplugin");
                    if (currentPlugin == null || currentPlugin.Installed == false)
                        return;
                }
            }
5 năm cách đây
Updated with complete code for nop 4.1:


        private static bool IsPluginInstalled(ITypeFinder typeFinder)
        {
            var thisPlugin = GetPluginInfo();
            if (thisPlugin == null) return false;

            var subscriptionService = new SubscriptionService();
            var eventPublisher = new EventPublisher(subscriptionService);
            var types = typeFinder.FindClassesOfType<IPluginFinder>();

            var enumerable = types as Type[] ?? types.ToArray();
            if (enumerable.Count() != 1) return false;
            if (!(Activator.CreateInstance(enumerable.First(), eventPublisher) is IPluginFinder pluginFinder))
                return false;
            var currentPlugin = pluginFinder.GetPluginDescriptorBySystemName(thisPlugin.SystemName);
            return currentPlugin != null && currentPlugin.Installed;

        }

        private static PluginDescriptor GetPluginInfo()
        {
            try
            {
                var pluginFullName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
                var pluginShortName = pluginFullName.Replace("Nop.Plugin.", string.Empty);
                var json = File.ReadAllText($"Plugins/{pluginShortName}/plugin.json");
                var descr = JsonConvert.DeserializeObject<PluginDescriptor>(json);
                return descr;
            }
            catch (Exception e)
            {
                if (Debugger.IsAttached)
                    Console.WriteLine(e);
                throw;
            }
        }
4 năm cách đây
Hi.
How to Upgrade for 4.20?
Please advice me.
Thanks.
4 năm cách đây
Hamidnch wrote:
Hi.
How to Upgrade for 4.20?
Please advice me.
Thanks.

NopCommerce 4.2 should not load uninstalled plugin dll. So any overridden methods should not be affected by uninstalled plugins.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.