Events 如何覆盖nopCommerce中现有的客户创建事件

Events 如何覆盖nopCommerce中现有的客户创建事件,events,plugins,nopcommerce,Events,Plugins,Nopcommerce,我使用的是nopCommerce版本3.5。每当任何客户注册自己时,我都会尝试创建一个默认的供应商帐户,并将其附加到已创建的客户。我已经创建了一个插件,我已经从管理部分安装。我已在日志中检查安装/卸载和注册方法是否执行。但我所写的主要方法并没有被解雇。我在清除缓存后尝试过,但没有成功 以下是我正在使用的代码: 文件夹结构: 1\Plugins\Nop.Plugin.Customer.Module\Controllers空 2\Plugins\Nop.Plugin.Customer.Module\

我使用的是nopCommerce版本3.5。每当任何客户注册自己时,我都会尝试创建一个默认的供应商帐户,并将其附加到已创建的客户。我已经创建了一个插件,我已经从管理部分安装。我已在日志中检查安装/卸载和注册方法是否执行。但我所写的主要方法并没有被解雇。我在清除缓存后尝试过,但没有成功

以下是我正在使用的代码:

文件夹结构: 1\Plugins\Nop.Plugin.Customer.Module\Controllers空

2\Plugins\Nop.Plugin.Customer.Module\Data Empty

3\Plugins\Nop.Plugin.Customer.Module\Domain空

4\Plugins\Nop.Plugin.Customer.Module\Infrastructure

文件:1 DependencyRegistrator.cs 5\Plugins\Nop.Plugin.Customer.Module\Services

文件:1 CustomerService.cs 6\Plugins\Nop.Plugin.Customer.Module

文件:1 CustomerModulePlugin.cs

namespace Nop.Plugin.Customer.Module
{
    public class CustomerModulePlugin : BasePlugin, IConsumer<EntityInserted<Nop.Core.Domain.Customers.Customer>>
    {

        private readonly Nop.Services.Vendors.IVendorService _vendorService;
        private readonly Nop.Services.Customers.ICustomerService _customerService;

        public override void Install()
        {

            base.Install();
        }

        public void HandleEvent(EntityInserted<Nop.Core.Domain.Customers.Customer> customer)
        {
            if (customer == null)
                throw new ArgumentNullException("customer");

            // make registered user as a vendor too.
            {
                //Nop.Core.Domain.Vendors.Vendor _vendor = new Nop.Core.Domain.Vendors.Vendor();
                var vendor = new Nop.Core.Domain.Vendors.Vendor()
                {
                    Name = customer.Entity.Username,
                    Email = customer.Entity.Email,
                    Active = true
                };


                this._vendorService.InsertVendor(vendor);
                customer.Entity.VendorId = vendor.Id;

                var registeredRole = _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.Vendors);
                if (registeredRole == null) throw new Exception("'Vendors' role could not be loaded");
                customer.Entity.CustomerRoles.Add(registeredRole);
            }
        }

        public override void Uninstall()
        {
            base.Uninstall();
        }
    }
}
文件:2 Description.txt

文件中的代码:

4\Plugins\Nop.Plugin.Customer.Module\Infrastructure

文件:1 DependencyRegistrator.cs

namespace Nop.Plugin.Tax.CountryStateZip.Infrastructure
{
    public class DependencyRegistrar : IDependencyRegistrar
    {
        public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {

             builder.RegisterType<Nop.Plugin.Customer.Module.Services.CustomerService>().As<Nop.Services.Customers.ICustomerService>().InstancePerRequest();

            ////we cache presentation models between requests
            //builder.RegisterType<CountryStateZipTaxProvider>()
            //    .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static"));
        }

        public int Order
        {
            get { return 1; }
        }
    }
}
5\Plugins\Nop.Plugin.Customer.Module\Services -文件:1 CustomerService.cs

namespace Nop.Plugin.Customer.Module.Services
{
    public class CustomerService : Nop.Services.Customers.CustomerService //Nop.Services.Customers.ICustomerService
    {

        #region Fields

        private readonly IRepository<Nop.Core.Domain.Customers.Customer> _customerRepository;
        private readonly IRepository<CustomerRole> _customerRoleRepository;
        private readonly IRepository<GenericAttribute> _gaRepository;
        private readonly IRepository<Order> _orderRepository;
        private readonly IRepository<ForumPost> _forumPostRepository;
        private readonly IRepository<ForumTopic> _forumTopicRepository;
        private readonly IRepository<BlogComment> _blogCommentRepository;
        private readonly IRepository<NewsComment> _newsCommentRepository;
        private readonly IRepository<PollVotingRecord> _pollVotingRecordRepository;
        private readonly IRepository<ProductReview> _productReviewRepository;
        private readonly IRepository<ProductReviewHelpfulness> _productReviewHelpfulnessRepository;
        private readonly IGenericAttributeService _genericAttributeService;
        private readonly IDataProvider _dataProvider;
        private readonly IDbContext _dbContext;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        private readonly CustomerSettings _customerSettings;
        private readonly CommonSettings _commonSettings;

        private readonly Nop.Services.Vendors.IVendorService _vendorService;
        private readonly Nop.Services.Customers.ICustomerService _customerService;

        #endregion



        public CustomerService(ICacheManager cacheManager,
            IRepository<Nop.Core.Domain.Customers.Customer> customerRepository,
            IRepository<CustomerRole> customerRoleRepository,
            IRepository<GenericAttribute> gaRepository,
            IRepository<Order> orderRepository,
            IRepository<ForumPost> forumPostRepository,
            IRepository<ForumTopic> forumTopicRepository,
            IRepository<BlogComment> blogCommentRepository,
            IRepository<NewsComment> newsCommentRepository,
            IRepository<PollVotingRecord> pollVotingRecordRepository,
            IRepository<ProductReview> productReviewRepository,
            IRepository<ProductReviewHelpfulness> productReviewHelpfulnessRepository,
            IGenericAttributeService genericAttributeService,
            IDataProvider dataProvider,
            IDbContext dbContext,
            IEventPublisher eventPublisher,
            CustomerSettings customerSettings,
            CommonSettings commonSettings)
            : base(
                cacheManager,
                customerRepository,
                customerRoleRepository,
                gaRepository,
                orderRepository,
                forumPostRepository,
                forumTopicRepository,
                blogCommentRepository,
                newsCommentRepository,
                pollVotingRecordRepository,
                productReviewRepository,
                productReviewHelpfulnessRepository,
                genericAttributeService,
                dataProvider,
                dbContext,
                eventPublisher,
                customerSettings,
                commonSettings
                )
        {
            this._cacheManager = cacheManager;
            this._customerRepository = customerRepository;
            this._customerRoleRepository = customerRoleRepository;
            this._gaRepository = gaRepository;
            this._orderRepository = orderRepository;
            this._forumPostRepository = forumPostRepository;
            this._forumTopicRepository = forumTopicRepository;
            this._blogCommentRepository = blogCommentRepository;
            this._newsCommentRepository = newsCommentRepository;
            this._pollVotingRecordRepository = pollVotingRecordRepository;
            this._productReviewRepository = productReviewRepository;
            this._productReviewHelpfulnessRepository = productReviewHelpfulnessRepository;
            this._genericAttributeService = genericAttributeService;
            this._dataProvider = dataProvider;
            this._dbContext = dbContext;
            this._eventPublisher = eventPublisher;
            this._customerSettings = customerSettings;
            this._commonSettings = commonSettings;
        }


        /// <summary>
        /// Insert a customer
        /// </summary>
        /// <param name="customer">Customer</param>
        public override void InsertCustomer(Nop.Core.Domain.Customers.Customer customer)
        {

            if (customer == null)
                throw new ArgumentNullException("customer");

            _customerRepository.Insert(customer);

            //event notification
            _eventPublisher.EntityInserted(customer);

            // make registered user as a vendor too.
            {
                //Nop.Core.Domain.Vendors.Vendor _vendor = new Nop.Core.Domain.Vendors.Vendor();
                var vendor = new Nop.Core.Domain.Vendors.Vendor()
                {
                    Name = customer.Username,
                    Email = customer.Email,
                    Active = true
                };

                this._vendorService.InsertVendor(vendor);
                customer.VendorId = vendor.Id;

                var registeredRole = _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.Vendors);
                if (registeredRole == null) throw new NopException("'Vendors' role could not be loaded");
                customer.CustomerRoles.Add(registeredRole);
            }

        }

    }
}
6\Plugins\Nop.Plugin.Customer.Module -文件:1 CustomerModulePlugin.cs

namespace Nop.Plugin.Customer.Module
{
    public class CustomerModulePlugin : BasePlugin, IConsumer<EntityInserted<Nop.Core.Domain.Customers.Customer>>
    {

        private readonly Nop.Services.Vendors.IVendorService _vendorService;
        private readonly Nop.Services.Customers.ICustomerService _customerService;

        public override void Install()
        {

            base.Install();
        }

        public void HandleEvent(EntityInserted<Nop.Core.Domain.Customers.Customer> customer)
        {
            if (customer == null)
                throw new ArgumentNullException("customer");

            // make registered user as a vendor too.
            {
                //Nop.Core.Domain.Vendors.Vendor _vendor = new Nop.Core.Domain.Vendors.Vendor();
                var vendor = new Nop.Core.Domain.Vendors.Vendor()
                {
                    Name = customer.Entity.Username,
                    Email = customer.Entity.Email,
                    Active = true
                };


                this._vendorService.InsertVendor(vendor);
                customer.Entity.VendorId = vendor.Id;

                var registeredRole = _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.Vendors);
                if (registeredRole == null) throw new Exception("'Vendors' role could not be loaded");
                customer.Entity.CustomerRoles.Add(registeredRole);
            }
        }

        public override void Uninstall()
        {
            base.Uninstall();
        }
    }
}
这将创建以下文件夹:

1\Presentation\Nop.Web\Plugins\Customer.Module

文件:1 Nop.Plugin.Customer.Module.dll

文件:2 Nop.Plugin.Customer.Module.pdb

文件:3 Description.txt

然后,我将生成的文件复制到nopCommerce发布的代码中

请让我知道我做错了什么

谢谢

找到了问题

实际上,在DependencyRegister.cs文件中,我错误地键入了以下内容:

命名空间Nop.Plugin.Tax.CountryStateZip.Infrastructure

而不是:

命名空间Nop.Plugin.Customer.Module.Infrastructure


已更正此问题。

您是否已验证您的服务正在替换默认的ICustomerService?您可以检查在其构造函数中设置断点