InvalidOperationException: Unable to resolve service for type 'Shop.Application.Services.ProductService' while attempting to activate 'Shop.Web.Controllers.HomeController'.
هنگامی که در HomeContorller از ProductRpository را اینجکت کردم تا از متد LastProducts استفاده کنم به خطای بالا برخوردم .
کاربرگرامی ! لطفا به پیام صوتی زیر گوش دهید.
سلام جناب مومنی
خدا قوت
بله در لایه ioc همه اینجکشن ها رو انجام دادم. منتها این ارور وقتی بوجود اومد که من درHomeController برای استفاده از اکشن LastProduct، ProductRepositoryرا اینجکت کردم.
کاربرگرامی ! لطفا به پیام صوتی زیر گوش دهید.
D
سلام جناب مومنی
ممنون از توضیحات جامع و کاملتون
از 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'.
مطالعه شود از injection هاتون داره ایراد میگیره
این هم 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
}
}
کاربرگرامی ! لطفا به پیام صوتی زیر گوش دهید.
J
مشکل حل شد .
private readonly ProductService _productService;
public HomeController(ProductService productService)
{
_productService = productService;
}
به جای IProductService از ProductService استفاده شده بود .
جناب مومنی سپاسگزارم .
خوش حال شدم
موفق باشید