• 1400/01/04

Error while validating the service descriptor :

سلام استاد. سال نو مبارک.

من پروژه رو با .Net Core 3.1 پیاده کردم، پکیج هی سازگار رو استفاده کردم و Dependencies اروری نداره. و وقتی Build  میکنم هم بدون مشکل انجام میشه.

 

اما در مرورگز این اورو رو نمایش میده:

An error occurred while starting the application.

AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Eshop.Core.IUserService Lifetime: Scoped ImplementationType: Eshop.Core.UserService': Unable to resolve service for type 'Eshop.Data.Repository.IGenericRepository`1[Eshop.Data.User]' while attempting to activate 'Eshop.Core.UserService'.)

که ارور  دقیقا در این خط از کد اتفاق میوفته:

services.AddScoped<IUserService, UserService>();

 

این مشکل رو چگونه میتونم حل کنم؟

 

 

PS:

Raw exception:

System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: Eshop.Core.IUserService Lifetime: Scoped ImplementationType: Eshop.Core.UserService': Unable to resolve service for type 'Eshop.Data.Repository.IGenericRepository`1[Eshop.Data.User]' while attempting to activate 'Eshop.Core.UserService'. ---> System.InvalidOperationException: Unable to resolve service for type 'Eshop.Data.Repository.IGenericRepository`1[Eshop.Data.User]' while attempting to activate 'Eshop.Core.UserService'.   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceDescriptor serviceDescriptor, CallSiteChain callSiteChain)   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValidateService(ServiceDescriptor descriptor)   --- End of inner exception stack trace ---   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValidateService(ServiceDescriptor descriptor)   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable`1 serviceDescriptors, IServiceProviderEngine engine, ServiceProviderOptions options)

اگر اطلاعات بیشتری برای پاسخ به این سوال لازم است، بفرمایید تا بررسی کنم.

 

  • 1400/01/05
  • ساعت 18:14

خودم مشکل رو پیدا کردم. جهت اطلاع سایرین اینجا هم قرار میدم.

در Startup.cs تعریف میکنیم که هر جا IUserServic خواسته شد، یک اینستنس از UserService بساز و ارایه بده. 

در  این خط:

services.AddScoped<IUserService, UserService>();

 

حالا در  داخل خود UserService در کانستراکتور، IGenericRepository خواسته شده.

همین  IGenericRepository  باید در  Startup.cs تعریف بشه که هرجا خواسته شد یک اینستنس از GenericRepository  بساز و بده.

در این خط:

services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));

 

من یک شاتباه تایپی اینجا داشتم و هر دو رو GenericRepository نوشته بودم. در زمان Build هیچ خطایی نمیداد. اما در هنگام عملیات، خبر نداشت که باید وقتی IGenericRepository  خواسته میشه چکار کنه. در نتیجه نمیتونست یک اینستنس از UserService بساز ه و میموند تو گِل.

 

 

با آرزوی موفقیت برای همه دوستان.

 

 


  • 1400/01/06
  • ساعت 19:00

سلام دوست من

در اموزش همین مورد قرار داده شده

فایل startup آموزش رو با دقت مطالعه کنید


logo-enamadlogo-samandehi