title: Configure Autofac DI container in ASP.NET CORE 3.1 Web API and consumer service from controller
body: <p>I need to configure Autofac DI container in ASP.NET CORE 3.1 Web API application and call register class from the container in Web API controller. I install Autofac.Extensions.DependencyInjection (6.0.0) and try to register container in my Startup.cs class but I am not able to use service. Also, do I need to configure the container in ConfigureServices(IServiceCollection services) class? The debugger does not hit IoCConfigurator() class after hitting point builder.RegisterModule(new IoCConfigurator());</p>
  <h2>Program.cs</h2>
  <pre><code> public static void Main(string[] args)
      {
          CreateHostBuilder(args).Build().Run();
      }
  
      public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
          Host.CreateDefaultBuilder(args)
              .UseServiceProviderFactory(new AutofacServiceProviderFactory())
              .ConfigureWebHostDefaults(webBuilder =&gt;
              {
                  webBuilder.UseStartup&lt;Startup&gt;();
              });
  }
  </code></pre>
  <h2>Startup.cs</h2>
  <pre><code> public class Startup
  {
      public IConfiguration Configuration { get; }
      public ContainerBuilder containerBuilder { get; }
  
      public Startup(IConfiguration configuration)
      {
          Configuration = configuration;
          containerBuilder = new ContainerBuilder();
      }
  
      public void ConfigureServices(IServiceCollection services)
      {
          ServicesConfigurator.Configure(services, Configuration);
          ConfigureIoC(services, containerBuilder);
      }
  
      public void ConfigureIoC(IServiceCollection services, ContainerBuilder builder)
      {
  
          builder.RegisterModule(new IoCConfigurator());
      }
  </code></pre>
  <h2>IoCConfigurator.cs</h2>
  <pre><code> public class IoCConfigurator: Module
  {
      protected override void Load(ContainerBuilder builder)
      {
          builder.RegisterType&lt;NotifyService&gt;().As&lt;INotificationService&gt;();
          builder.RegisterType&lt;UsersService&gt;().AsSelf();
      }
   }
  </code></pre>
  <h2>INotification Interface &amp; Class</h2>
  <pre><code> public interface INotificationService
  {
     void notifyUsernameChanged(Users users);
  }
  
     public class NotifyService : INotificationService
  {
      public void notifyUsernameChanged(Users users)
      {
          string changedUsername = users.Username;
  
          Console.WriteLine(&quot;Username has changed to ... &quot;);
          Console.WriteLine(changedUsername);
      }
  }
  </code></pre>
  <h2>User &amp; User Service Class</h2>
  <pre><code> public class Users
  {
      public string Username { get; set; }
  
      public Users(string username)
      {
          this.Username = username;
      }  
  }
  
  public class UsersService
  {
      private INotificationService _notificationService;
      public UsersService(INotificationService notificationService)
      {
          this._notificationService = notificationService;
      }
  
      public void ChangeUsername(Users users, string newUsername)
      {
          users.Username = newUsername;
          _notificationService.notifyUsernameChanged(users);
      }
  }
  </code></pre>
  <p>API Controller where I want to class the UserService Class and get reference from DI container</p>
  <pre><code>[Authorize]
  [Route(&quot;txn/v1/[controller]/[action]&quot;)]
  [ApiController]
  public class DashboardController : ControllerBase
  {
  
    [HttpPost(&quot;{name}&quot;)]
      public ActionResult&lt;HelloMessage&gt; GetMessage(string name)
      {
          // call container here...
          var result = new HelloMessage()
          {
              GivenName = name,
              ReturnMessage = &quot;Dashboard@ Hello, Welcome to Texanite Digital&quot;
          };
  
          return result;
      }
  </code></pre>