• 1403/02/07

مشکل در نمایش محصولات جدید :

InvalidOperationException: Unable to resolve service for type 'Shop.Application.Services.ProductService' while attempting to activate 'Shop.Web.Controllers.HomeController'.

هنگامی که در HomeContorller  از ProductRpository را اینجکت کردم تا از متد LastProducts استفاده کنم به خطای بالا برخوردم .

  • 1403/02/16
  • ساعت 16:24

کاربرگرامی ! لطفا به پیام صوتی زیر گوش دهید.


  • 1403/02/16
  • ساعت 16:49

سلام جناب مومنی

خدا قوت

بله در لایه ioc همه اینجکشن ها رو انجام دادم. منتها این ارور وقتی بوجود اومد که من درHomeController برای استفاده از اکشن LastProduct، ProductRepositoryرا اینجکت کردم. 


  • 1403/02/16
  • ساعت 16:54

کاربرگرامی ! لطفا به پیام صوتی زیر گوش دهید.

D


  • 1403/02/17
  • ساعت 17:24

سلام جناب مومنی

ممنون از توضیحات جامع و کاملتون 

از PrdocutService استفاده کردم در HomeController

 public class HomeController : SiteBaseController
    {
        #region constructor
        private readonly ProductService _productService;
        public HomeController(ProductService productService)
        {
            _productService = productService;
        }
        #endregion
        public async Task<IActionResult> Index()
        {
            ViewData["LastProducts"] = await _productService.LastProduct();
            return View();
        }

        public IActionResult Error()
        {
            return View();
        }
    }

همچنین LastProduct  در ProductRepository

        public async Task<List<ProductItemViewModel>> LastProduct()
        {
            var lastProduct = await _context.Products.Include(c=>c.ProductComments)
                .Include(c => c.ProductSelectedCategories).ThenInclude(c => c.ProductCategory)
                .AsQueryable()
                .OrderByDescending(c => c.CreateDate)
                .Select(c => new ProductItemViewModel
                {
                    ProductCategory = c.ProductSelectedCategories.Select(c => c.ProductCategory).First(),
                    CommentCount = c.ProductComments.Count(),
                    Price = c.Price,
                    ProductId = c.Id,
                    ProductImageName = c.ProductImageName,
                    ProductName = c.Name
                }).Take(8).ToListAsync();
            return lastProduct;
        }

بالا بد بیان کردم گفتم  ProductRepository  

همچنان همون ارور میده

InvalidOperationException: Unable to resolve service for type 'Shop.Application.Services.ProductService' while attempting to activate 'Shop.Web.Controllers.HomeController'.


  • 1403/02/17
  • ساعت 17:29

این هم DependencyContainer

public class DependencyContainer
    {
        public static void RegisterService(IServiceCollection services)
        {

            #region services
            services.AddScoped<IUserService, UserService>();
            services.AddScoped<IWalletService, WalletService>();
            services.AddScoped<IProductService,ProductService>();
            services.AddScoped<ISiteSettingService, SiteSettingService>();
            services.AddScoped<IOrderService, OrderService>();

            #endregion


            #region repositories 
            services.AddScoped<IUserRepository, UserRepository>();
            services.AddScoped<IWalletRepository, WalletRepository>();
            services.AddScoped<IProductRepository, ProductRepository>();
            services.AddScoped<ISiteSettingRepository,SiteSettingRepository>();
            services.AddScoped<IOrderRepository, OrderRepository>();

            #endregion


            #region tools 
            services.AddScoped<IPasswordHelper, PasswordHelper>();
            services.AddSingleton<HtmlEncoder>(HtmlEncoder.Create(allowedRanges: new[] { UnicodeRanges.BasicLatin, UnicodeRanges.Arabic }));
            services.AddScoped<ISmsService, SmsService>();

            #endregion
        }
    }

  • 1403/02/17
  • ساعت 20:53

کاربرگرامی ! لطفا به پیام صوتی زیر گوش دهید.

J


  • 1403/02/17
  • ساعت 21:14

مشکل حل شد .

    private readonly ProductService _productService;
        public HomeController(ProductService productService)
        {
            _productService = productService;
        }

به جای IProductService  از ProductService استفاده شده بود . 

جناب مومنی سپاسگزارم .


  • 1403/02/17
  • ساعت 21:44

خوش حال شدم 

موفق باشید


logo-samandehi