I have 3 Project:
- 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.

services.AddReposioryConnector(Configuration);fromAddServiceConnector