2

I have 3 Project:

enter image description here

  • API Project > Which includes Controller
  • DataLayer Project > Which includes my Repository, Interface, ADO.NET Entity, Context.(Class Library Template .NET Framwork)
  • Services Project > Which includes my Services, Interface, you can say Business Logic.(Class Library Template .NET Framwork)

So My API Project(Controller) will call Services(Business Layer) then DataLayer Project(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)

I have tried below steps: Added each file in Project with below code:

Service Project:

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;
    }
}

DataLayer Project:

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;
    }
}

Then in 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);

    }

But still no luck. Can anyone suggest where the mistake is.

5
  • Do you have the same error? Commented May 15, 2021 at 22:30
  • Also I would say that you should remove services.AddReposioryConnector(Configuration); from AddServiceConnector Commented May 15, 2021 at 22:32
  • @GuruStron Its same actually. Just a another way of writing. Also i removed but no luck here Commented May 16, 2021 at 18:02
  • 1
    Can you please provide minimal reproducible example Commented May 17, 2021 at 13:30
  • @NikhilChaudhary Just wondering should be be registering the Repository connector in the startup? If so, you are directory referencing the DAL, which shouldn't be the case. DAL should only be accessible by ServiceLayer. Isn't it? Commented Jan 30, 2022 at 18:10

1 Answer 1

1

Thanks for reviewing this. It got resolved now. Resolution: I was previously using Class Library(.NET Framework) in my Service and Data layer project. I changed these projects to Class Library(.NET Core) and it got worked.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.