Unable to resolve service for type
‘AuthMicroservice.Repository.IAuthRepo’ while attempting to activate
‘AuthMicroservice.Controllers.AuthController’. at
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider
sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
at lambda_method4(Closure , IServiceProvider , Object[] ) at
Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass7_0.b__0(ControllerContext
controllerContext) at
Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass6_0.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.g__Awaited|20_0(ResourceInvoker
invoker, Task lastTask, State next, Scope scope, Object state, Boolean
isCompleted) at
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker
invoker, Task task, IDisposable scope) at
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker
invoker, Task task, IDisposable scope) at
Microsoft.AspNetCore.Routing.EndpointMiddleware.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)
Please look at these code files
AuthController.cs
using AuthMicroservice.Models;
using AuthMicroservice.Repository;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Web.Http.Cors;
namespace AuthMicroservice.Controllers
{
[Route("api/[controller]")]
[ApiController]
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class AuthController : ControllerBase
{
private IConfiguration _config;
static readonly log4net.ILog _log4net = log4net.LogManager.GetLogger(typeof(AuthController));
private readonly IAuthRepo repo;
public AuthController(IConfiguration config, IAuthRepo _repo)
{
_config = config;
repo = _repo;
}
/// <summary>
/// Post method for Login
/// </summary>
/// <param name="login"></param>
/// <returns></returns>
[HttpPost]
public IActionResult Login([FromBody] Auth login)
{
RFQRepo auth_repo = new RFQRepo(_config, repo);
_log4net.Info("Login initiated!");
IActionResult response = Unauthorized();
//login.FullName = "user1";
var user = auth_repo.AuthenticateUser(login);
if (user == null)
{
return NotFound();
}
else
{
var tokenString = auth_repo.GenerateJSONWebToken(user);
response = Ok(new { token = tokenString });
}
return response;
}
}
}
IAuthRepo.cs
using AuthMicroservice.Models;
namespace AuthMicroservice.Repository
{
public interface IAuthRepo
{
public Auth GetRFQCred(Auth cred);
}
}
AuthRepo.cs
using AuthMicroservice.Models;
using AuthMicroservice.Provider;
namespace AuthMicroservice.Repository
{
public class AuthRepo : IAuthRepo
{
private readonly IAuthProvider provider;
public AuthRepo(IAuthProvider _provider)
{
provider = _provider;
}
public Auth? GetRFQCred(Auth cred)
{
if (cred == null)
{
return null;
}
Auth rfq = provider.GetRFQ(cred);
return rfq;
}
}
}
2
Answers
you should define that in a start-up like :
or in minimal API :
Make sure you are register your
IAuthRepo
service with service collection inStartup
class: