Saya punya 3 Proyek:

enter image description here

  • Proyek API > Yang termasuk Pengontrol
  • Proyek DataLayer > Yang termasuk Repositori saya, Antarmuka, Entitas ADO.NET, Konteks. (Templat Perpustakaan Kelas .NET Framwork)
  • Proyek Layanan > Yang mencakup Layanan saya, Antarmuka, Anda dapat mengatakan Logika Bisnis. (Templat Perpustakaan Kelas .NET Framwork)

Jadi Proyek API Saya(Pengontrol) akan memanggil Layanan(LapisanBisnis) lalu Proyek DataLayer(DbContext).

But when I run my project I am getting:
System.InvalidOperationException: Unable to resolve service for type 'ABCProject.Services.ServiceInterface.IRecruiterService' while attempting to activate 'ABCProject.API.Controllers.RecruiterController'.
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
   at lambda_method3(Closure , IServiceProvider , Object[] )
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass4_0.<CreateActivator>b__0(ControllerContext controllerContext)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Saya telah mencoba langkah-langkah di bawah ini: Menambahkan setiap file di Proyek dengan kode di bawah ini:

Proyek Layanan:

public static class ServiceCollectionExtensions
{
    public static IConfiguration Configuration { get; set; }
    public static IServiceCollection AddServiceConnector(this IServiceCollection services)
    {
        services.AddTransient<IRecruiterService, RecruiterService>();
        services.AddReposioryConnector(Configuration);
        return services;
    }
}

Proyek DataLayer:

public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddReposioryConnector(this IServiceCollection services, IConfiguration configuration)
    {
        services.AddTransient<IRecruiterRepository, RecruiterRepository>();
        services.AddDbContext<DNCLContext>(options =>
            options.UseSqlServer(configuration.GetConnectionString("DBConnection")));
        return services;
    }
}

Kemudian di API > Startup.cs:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "ABCProject.API", Version = "v1" });
        });
       
        services.AddServiceConnector();
        services.AddReposioryConnector(Configuration);

    }

Tapi masih belum beruntung. Adakah yang bisa menyarankan di mana kesalahannya.

0
Nikhil Chaudhary 16 Mei 2021, 01:26

2 jawaban

Jawaban Terbaik

Terima kasih telah meninjau ini. Ini sudah diselesaikan sekarang. Resolusi: Saya sebelumnya menggunakan Class Library(.NET Framework) dalam proyek lapisan Layanan dan Data saya. Saya mengubah proyek ini menjadi Class Library(.NET Core) dan berhasil.

0
Nikhil Chaudhary 17 Mei 2021, 20:39

ServiceCollectionExtensions.Configuration tidak disetel ke apa pun ketika

services.AddServiceConnector();

Dipanggil di API.

Yang akan menyebabkan pengecualian referensi nol yang ditangguhkan ketika

options.UseSqlServer(configuration.GetConnectionString("DBConnection"))

Dipanggil melalui services.AddReposioryConnector(Configuration);

Saya akan menyarankan refactoring ke

public static class ServiceCollectionExtensions {
    public static IServiceCollection AddServiceConnector(this IServiceCollection services, IConfiguration Configuration) {
        services.AddTransient<IRecruiterService, RecruiterService>();
        services.AddReposioryConnector(Configuration);
        return services;
    }
}

Dan juga menghapus panggilan kedua ke services.AddReposioryConnector saat memulai

public void ConfigureServices(IServiceCollection services) {
    services.AddControllers();
    services.AddSwaggerGen(c => {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "ABCProject.API", Version = "v1" });
    });
   
    services.AddServiceConnector(Configuration);
    
    //...
}

Karena AddServiceConnector sudah memanggil AddReposioryConnector

0
Nkosi 15 Mei 2021, 22:35