• 1404/04/06
  • 0
  • 193
1404/04/06 |
dot-net
asp-net-core

خطا در ثبت سرویس در Program

با سلام

در انجام پروژه مقدماتی در دات نت 8 در Program وقتی ثبت سرویس انجام میدم خطا رخ میده

System.AggregateException HResult=0x80131500 Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: KalaMarket_Store.Application.Services.Users.Queries.GetUsers.IGetUsersService Lifetime: Scoped ImplementationType: KalaMarket_Store.Application.Services.Users.Queries.GetUsers.GetUsersService': Unable to resolve service for type 'KalaMarket_Store.Application.Interfaces.Contexts.IDataBaseContext' while attempting to activate 'KalaMarket_Store.Application.Services.Users.Queries.GetUsers.GetUsersService'.) Source=Microsoft.Extensions.DependencyInjection StackTrace: at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options) at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build() at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build() at Program.<Main>$(String[] args) in E:\MyProjects\1404-04-04\KalaMarket_Store\EndPoint.Site\Program.cs:line 21 This exception was originally thrown at this call stack: [External Code] Inner Exception 1: InvalidOperationException: Error while validating the service descriptor 'ServiceType: KalaMarket_Store.Application.Services.Users.Queries.GetUsers.IGetUsersService Lifetime: Scoped ImplementationType: KalaMarket_Store.Application.Services.Users.Queries.GetUsers.GetUsersService': Unable to resolve service for type 'KalaMarket_Store.Application.Interfaces.Contexts.IDataBaseContext' while attempting to activate 'KalaMarket_Store.Application.Services.Users.Queries.GetUsers.GetUsersService'. Inner Exception 2: InvalidOperationException: Unable to resolve service for type 'KalaMarket_Store.Application.Interfaces.Contexts.IDataBaseContext' while attempting to activate 'KalaMarket_Store.Application.Services.Users.Queries.GetUsers.GetUsersService'.

ممنون میشم منو راهنمایی کنید


علت خطا:
اینترفیس ‎IDataBaseContext‎ را در DI ثبت نکرده‌اید و سرویس ‎GetUsersService‎ بدون آن ساخته نمی‌شود.

راه‌حل سریع:

builder.Services.AddDbContext<DataBaseContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

builder.Services.AddScoped<IDataBaseContext>(p =>
    p.GetRequiredService<DataBaseContext>());

builder.Services.AddScoped<IGetUsersService, GetUsersService>();


اول کانتکست و اینترفیسش را ثبت کنید، بعد سرویس‌هایی که از آن استفاده می‌کنند؛ خطا برطرف می‌شود.



پاسخی برای این باگ ارسال کنید

لطفا کدها را درون قطعه کد ارسال نمایید

برای ارسال پاسخ باید وارد حساب کاربری خود شوید
ورود به حساب کاربری