Is not assignable to service 'Nop.Services.Catalog.ICategoryService'.'

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
4 years ago
Hello, I have a problem with service
Error: System.ArgumentException: 'The type 'Nop.Plugin.Widgets.MRMegaMenuStandard.Services.MRMegaMenuStandardService' is not assignable to service 'Nop.Services.Catalog.ICategoryService'.'

View component:

[ViewComponent(Name = "MRMegaMenuStandard")]
    public class MRMegaMenuStandardViewComponent : NopViewComponent
    {
        private readonly ICategoryService _categoryService;
        private readonly IMRMegaMenuStandardService _mRMegaMenuStandardService;
        private readonly IWorkContext _workContext;

        public MRMegaMenuStandardViewComponent(IWorkContext workContext,
        IMRMegaMenuStandardService mRMegaMenuStandardService,
        ICategoryService categoryService)
        {
            _workContext = workContext;
            _categoryService = categoryService;
        }

        public IViewComponentResult Invoke()
        {
            return Content("");
        }
    }

Service:

public interface IMRMegaMenuStandardService
    {
        /// <summary>
        /// Logs the specified record.
        /// </summary>
        /// <param name="record">The record.</param>
    }

    public class MRMegaMenuStandardService : IMRMegaMenuStandardService
    {
        private readonly IRepository<ProductCategory> _productCategoryRepository;

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="productCategoryRepository"></param>
        public MRMegaMenuStandardService(IRepository<ProductCategory> productCategoryRepository)
        {
            _productCategoryRepository = productCategoryRepository;
        }
    }
4 years ago
I think have not registered the dependency. To do that create a DependencyRegistrar class as below.

public class DependencyRegistrar : IDependencyRegistrar
{
    public void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
    {
        builder.RegisterType<YourClass>().As<YourInterface>().InstancePerLifetimeScope();
    }

    public int Order => 1;
}


In your case

public class DependencyRegistrar : IDependencyRegistrar
{
    public void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
    {
        builder.RegisterType<MRMegaMenuStandardService>().As<IMRMegaMenuStandardService>().InstancePerLifetimeScope();
    }

    public int Order => 1;
}
4 years ago
How does your DependencyRegistrar class look? Make sure you have implemented it as
builder.RegisterType<CONCRETE_CLASS>().As<INTERFACE>(); 
4 years ago
Hi, I want to assign to existing interface ICategoryService.

builder.RegisterType<MRMegaMenuStandardService>().As<ICategoryService>().InstancePerLifetimeScope();
4 years ago
You need to implement the ICategoryService in the MRMegaMenuStandardService class if you want to use the existing interface.
4 years ago
Ok it's working now. There is some better way to implement all functionality of ICategoryService than copy full CategoryService?:

public interface IMRMegaMenuStandardService : ICategoryService
    {
        /// <summary>
        /// Logs the specified record.
        /// </summary>
        /// <param name="record">The record.</param>
    }

    public partial class MRMegaMenuStandardService : IMRMegaMenuStandardService
    {
        #region Fields

        private readonly CatalogSettings _catalogSettings;
        private readonly CommonSettings _commonSettings;
        private readonly IAclService _aclService;
        private readonly ICacheManager _cacheManager;
        private readonly IDataProvider _dataProvider;
        private readonly IDbContext _dbContext;
        private readonly IEventPublisher _eventPublisher;
        private readonly ILocalizationService _localizationService;
        private readonly IRepository<AclRecord> _aclRepository;
        private readonly IRepository<Category> _categoryRepository;
        private readonly IRepository<Product> _productRepository;
        private readonly IRepository<ProductCategory> _productCategoryRepository;
        private readonly IRepository<StoreMapping> _storeMappingRepository;
        private readonly IStaticCacheManager _staticCacheManager;
        private readonly IStoreContext _storeContext;
        private readonly IStoreMappingService _storeMappingService;
        private readonly IWorkContext _workContext;

        #endregion

        #region Ctor

        public MRMegaMenuStandardService(CatalogSettings catalogSettings,
            CommonSettings commonSettings,
            IAclService aclService,
            ICacheManager cacheManager,
            IDataProvider dataProvider,
            IDbContext dbContext,
            IEventPublisher eventPublisher,
            ILocalizationService localizationService,
            IRepository<AclRecord> aclRepository,
            IRepository<Category> categoryRepository,
            IRepository<Product> productRepository,
            IRepository<ProductCategory> productCategoryRepository,
            IRepository<StoreMapping> storeMappingRepository,
            IStaticCacheManager staticCacheManager,
            IStoreContext storeContext,
            IStoreMappingService storeMappingService,
            IWorkContext workContext)
        {
            _catalogSettings = catalogSettings;
            _commonSettings = commonSettings;
            _aclService = aclService;
            _cacheManager = cacheManager;
            _dataProvider = dataProvider;
            _dbContext = dbContext;
            _eventPublisher = eventPublisher;
            _localizationService = localizationService;
            _aclRepository = aclRepository;
            _categoryRepository = categoryRepository;
            _productRepository = productRepository;
            _productCategoryRepository = productCategoryRepository;
            _storeMappingRepository = storeMappingRepository;
            _staticCacheManager = staticCacheManager;
            _storeContext = storeContext;
            _storeMappingService = storeMappingService;
            _workContext = workContext;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Delete category
        /// </summary>
        /// <param name="category">Category</param>
        public virtual void DeleteCategory(Category category)
        {
            if (category == null)
                throw new ArgumentNullException(nameof(category));

            if (category is IEntityForCaching)
                throw new ArgumentException("Cacheable entities are not supported by Entity Framework");

            category.Deleted = true;
            UpdateCategory(category);

            //event notification
            _eventPublisher.EntityDeleted(category);

            //reset a "Parent category" property of all child subcategories
            var subcategories = GetAllCategoriesByParentCategoryId(category.Id, true);
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.