Plugin Development - unable to convert plugin descriptor to its interface

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 anos atrás
NopCommerce Version - 2.65

I am trying to develop a plugin for MediaNet API, which helps to purchase a product using Media Net API  from their server. after the order is processed by NopCommerce, and then a download link is sent to the user. Here are the steps I performed.

1) Nop.Services
(a) A new folder "Vendor" is created.
(b) IVendorAPIProvider.cs is created under "Vendor".
(c) IVendorAPIService.cs is created under "Vendor".
(d) VendorAPIService.cs is created under "Vendor".
(e) OrderProcessingService.cs is modified.

2) Nop.Plugin.API.MediaNet, a new project is created under Plugins folder, output of which is set as per required path in debug and release mode both.

3) DependencyRegistrar.cs is modified to register VendorAPIService.

Here's code for each file

IVendorAPIProvider.cs


  public partial interface IVendorAPIProvider : IPlugin
    {
        Order Purchase(Order order, string userDomain, string userIP);
        //although it is called internally, lets keep it here , may be used in some apis explicitly
        Order GetDownloadLocations(Order order, string vendorOrderID);
        //not implemented
        bool VerifyItemExistsForVendor(string ItemID, int ItemType);
    }


IVendorAPIService.cs


public partial interface IVendorAPIService
    {
        IVendorAPIProvider LoadVendorAPIServiceProviderBySystemName(string SystemName);
        IList<IVendorAPIProvider> LoadAllProviders();
    }


VendorAPIService.cs


public partial class VendorAPIService : IVendorAPIService
    {
        private readonly IPluginFinder _pluginFinder;
        public VendorAPIService(IPluginFinder pluginFinder)
        {
            this._pluginFinder = pluginFinder;
        }

        public IVendorAPIProvider LoadVendorAPIServiceProviderBySystemName(string SystemName)
        {
            var descriptor = _pluginFinder.GetPluginDescriptorBySystemName<IVendorAPIProvider>(SystemName);
            if (descriptor != null)
                return descriptor.Instance<IVendorAPIProvider>();
            return null;
        }

        public IList<IVendorAPIProvider> LoadAllProviders()
        {
            return _pluginFinder.GetPlugins<IVendorAPIProvider>().ToList();
        }
    }


OrderProcessingService.cs

OrderProcessingService constructor is modified, _vendorAPIService of type IVendorAPIService is passed as an additional paramter,  Just after this line of code, _eventPublisher.PublishOrderPlaced(order); I have put the following code.


IVendorAPIProvider _prov =  _vendorAPIService.LoadVendorAPIServiceProviderBySystemName("VND001");
order = _prov.Purchase(order, "ghhCommerce.com", "127.0.0.1");
_orderService.UpdateOrder(order);


In Nop.Plugin.API.MediaNet Project,  

MNetAPIProvider.cs


public partial class MNetAPIProvider : BasePlugin, IVendorAPIProvider
    {
            public MNetAPIProvider()
            {

            }

           public Core.Domain.Orders.Order Purchase(Core.Domain.Orders.Order order, string userDomain, string userIP)
           {
               //code implemented.
           }
        public Core.Domain.Orders.Order GetDownloadLocations(Core.Domain.Orders.Order order, string vendorOrderID)
        {
            throw new NotImplementedException();
        }

        public bool VerifyItemExistsForVendor(string ItemID, int ItemType)
        {
            throw new NotImplementedException();
        }
    }


Contents of Description.txt for the new plugin

Group: Miscellaneous
FriendlyName: MediaNetCart
SystemName: VND001
Version: 1.00
SupportedVersions: 2.65
Author: neeraj
DisplayOrder: 1
FileName: Nop.Plugin.API.MediaNet.dll


DependencyRegistrar.cs

In Register method, I added

builder.RegisterType<VendorAPIService>().As<IVendorAPIService>().InstancePerHttpRequest();


Everything compiles fine, but here's the problem which I am facing

in OrderProcessingService.cs, at the following line of code

IVendorAPIProvider _prov =  _vendorAPIService.LoadVendorAPIServiceProviderBySystemName("VND001");


it is returning NULL. I have checked the code by debugging, its showing the new plugin detected and installed in _pluginFinder object in VendorAPIService. The only issue which seems to me that its not able to convert MediaNet object to IVendorAPIProvider. See this line of code

var descriptor = _pluginFinder.GetPluginDescriptorBySystemName<IVendorAPIProvider>(SystemName);


If I use it like this without IVendorAPIProvider

var descriptor = _pluginFinder.GetPluginDescriptorBySystemName(SystemName);


It give me result of MediaNetCart plugin, which seems to be correct, only when I used IVendorAPIProvider, it returns me NULL.

Please help me, how to get the IVendorAPIProvider value instead of NULL.

I have tried to explain my question as best as I can, still if any confusion, please tell me, I will try to explain it more.

Thanks
11 anos atrás
Try registering MNetAPIProvider to IVendorAPIProvider in your DependencyRegistrar? :D
11 anos atrás
Thanks wooncherk for your quick reply, but can you explain it more. I have no separate dependency registrar, please help.
11 anos atrás
In your plugin project, you have this line

builder.RegisterType<VendorAPIService>().As<IVendorAPIService>().InstancePerHttpRequest();


Try registering for MNetApiProvider in the same file. :D
11 anos atrás
I have to create reference to register that into already existing DependencyRegistrar, which has not been done for existing plugins.

So, I added a new file into my MediaNet plugin project, called DependencyRegistrar.cs


public class DependencyRegistrar : IDependencyRegistrar
    {

        public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            builder.RegisterType<MNetAPIProvider>().As<IVendorAPIService>().InstancePerHttpRequest();
            
        }

        public int Order
        {
            get { return 0; }
        }
        
    }


but the things still not working for me. Am I following the correct way to add a plugin ?
11 anos atrás
Thanks a lot for your help, I got it working. I stopped the local server, clean the solution and then run again , and everything seems to be working. It seems that it was cached.

Can you tell me why do I need to register this separately. That means if I am going to implement another provider, like Nexway, then I need to register it separately too.  I have tried to follow the ITaxProvider, ITaxService which are further used by Nop.Plugin.Tax.Free and Nop.Plugin.Tax.FixedRate, and these are not registered in dependencyregistrar or may be I am wrong. Please explain.

Also, can you tell me whether it is correct approach or there may be something better in my scenario as per architecture of NopCommerce ?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.