title: ServiceLocator with Autofac in asp.net core 3.1
body: <p>I'm developing an asp.net core 3.1 webapi application and i'm using Autofac as DI container.
  For one particular case i cannot use ConstructorInjection nor propertyinjection nor methodinjection. My only way is to implement a sort of ServiceLocator pattern with the support of Autofac.</p>
  <p>*I known that the service locator is an antipattern, but i will use that only if it will be the only chance *</p>
  <p>Said that, I create a little static class :</p>
  <pre><code>public static class ServiceLocator
  {
  
      private static XXXX Resolver;
  
      public static T Resolve&lt;T&gt;()
      {
          return Resolver.Resolve&lt;T&gt;();
      }
  
      public static void SetCurrentResolver(XXXX resolver)
      {
          Resolver = resolver;
      }
  }
  </code></pre>
  <p>I write XXXX on the type of Resolver property because i don't know which is the Autofac class to use. The method SetCurrentResolver will be called in the Configure method of Startup.cs</p>
  <pre><code>    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILifetimeScope serviceProvider)
      {
          //OTHER STUFF NOT RELATED HERE
  
          ServiceLocator.SetCurrentResolver(serviceProvider);
      }
  </code></pre>
  <p>I tried to pass the instance of ILifetimeScope but when i use it later in the service locator it will be Disposed and then not work. I thinked to pass an IContainer object but i'm not able to retrieve an instance in Startup.cs (neither in the <strong>Configure</strong> method nor in the <strong>ConfigureContainer</strong>)</p>
  <p>I Report the ConfigureContainer method for completion</p>
  <pre><code>    public void ConfigureContainer(ContainerBuilder builder)
      {
          //first register the dependency for WebApi Project
  
          builder.RegisterType&lt;HttpContextUserService&gt;().As&lt;IUserService&gt;().SingleInstance();
  
          //and then register the dependency for all other project
  
          var appConfiguration = new AppConfiguration();
          Configuration.GetSection(&quot;Application&quot;).Bind(appConfiguration);
          builder.RegisterInstance(appConfiguration).SingleInstance();
  
          builder.RegisterModule(new DependencyInjectionBootstrapper(appConfiguration)); 
          
      }
  </code></pre>
  <p>Anyone can help me with this problem?</p>
  <p>Thanks</p>