title: ASP.NET Core 6 Dependency injection in controller return System.InvalidOperationException: Unable to resolve service
body: <p>I'm trying to develop a Web API project with ASP.NET Core 6, but I get this error, there seems to be an error in the dependency injection, but I haven't found this:</p>
  <blockquote>
  <p>System.InvalidOperationException: Unable to resolve service for type 'api.Interface.IUsersService' while attempting to activate 'api.Controllers.UsersController'.</p>
  <p>at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)<br />
  at lambda_method3(Closure , IServiceProvider , Object[] )<br />
  at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.&lt;&gt;c__DisplayClass7_0.b__0(ControllerContext controllerContext)<br />
  at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.&lt;&gt;c__DisplayClass6_0.g__CreateController|0(ControllerContext controllerContext)
  at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; 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)</p>
  </blockquote>
  <p><code>Startup.cs</code> :</p>
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
         string connectionString = Configuration.GetConnectionString(&quot;DefaultConnection&quot;);
         services.AddDbContext&lt;DataContext&gt;(options =&gt; options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
         services.AddCors(options =&gt; options.AddPolicy(&quot;ApiCorsPolicy&quot;, build =&gt;
          {
              build.WithOrigins(&quot;http://localhost:8080&quot;)
                   .AllowAnyMethod()
                   .AllowAnyHeader();
          }));
          services.AddMvc();
          services.AddControllers();
          services.AddScoped&lt;IJwtUtils, JwtUtils&gt;();
          services.AddScoped&lt;IUsersService, UsersService&gt;();
          services.AddScoped&lt;IQrcodesService, QrcodesService&gt;();
          services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
          services.Configure&lt;AppSettings&gt;(Configuration.GetSection(&quot;AppSettings&quot;));
  
          services.AddControllers().AddJsonOptions(options =&gt;
          {
              options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
          });
  }
  </code></pre>
  <p><code>IUsersService.cs</code>:</p>
  <pre><code>using api.Response;
  using System.Collections.Generic;
  
  namespace api.Interface
  {
      public interface IUsersService
      {
          // ...
      }
  }
  </code></pre>
  <p><code>UsersService.cs</code>:</p>
  <pre><code>using api.Authorization;
  using api.Helpers;
  using api.Interface;
  using api.Response;
  using AutoMapper;
  using System.Collections.Generic;
  using System.Linq;
  using BCryptNet = BCrypt.Net.BCrypt;
  
  namespace api.Services
  {
      public class UsersService : IUsersService
      {
          private DataContext _context;
          private IJwtUtils _jwtUtils;
          private readonly IMapper _mapper;
  
          public UsersService(
              DataContext context,
              IJwtUtils jwtUtils,
              IMapper mapper)
          {
              _context = context;
              _jwtUtils = jwtUtils;
              _mapper = mapper;
          }
  
          // ...
      }
  }
  </code></pre>
  <p><code>UsersController.cs</code>:</p>
  <pre><code>using api.Helpers;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.Extensions.Options;
  using api.Response;
  using api.Authorization;
  using api.Interface;
  
  namespace api.Controllers
  {
      [Authorize]
      [ApiController]
      [Route(&quot;[controller]&quot;)]
      public class UsersController : ControllerBase
      {
          private readonly IUsersService _userService;
          private readonly AppSettings _appSettings;
  
          public UsersController(
              IUsersService userService,
              IOptions&lt;AppSettings&gt; appSettings)
          {
              _userService = userService;
              _appSettings = appSettings.Value;
          }
  
          // ...
      }
  }
  </code></pre>
  <p><code>DataContext.cs</code>:</p>
  <pre><code>using System;
  using Microsoft.EntityFrameworkCore;
  using api.Response;
  using BCryptNet = BCrypt.Net.BCrypt;
  using api.Authorization;
  
  namespace api.Helpers
  {
      public class DataContext : DbContext
      {
          public DataContext() { }
  
          public DataContext(DbContextOptions&lt;DataContext&gt; options) : base(options) { }
  
          public DbSet&lt;User&gt; Users { get; set; }
          public DbSet&lt;Role&gt; Roles { get; set; }
          public DbSet&lt;QRcode&gt; QRcodes { get; set; }
          public DbSet&lt;Activity&gt; Activities { get; set; }
          public DbSet&lt;Giustification&gt; Giustifications  { get; set; }
         
          // ...
      }
  }
  </code></pre>
  <p><code>JwtUtils.js</code>:</p>
  <pre><code>public class JwtUtils : IJwtUtils
  {
      private readonly AppSettings _appSettings;
  
      public JwtUtils(IOptions&lt;AppSettings&gt; appSettings)
      {
          _appSettings = appSettings.Value;
      }
             //...
  }
  </code></pre>
  <p>I'm using pomelo 6.0.1 with mysql '8.0.27' and .NET 6.</p>