title: How to inject IHttpContextAccessor into Autofac TenantIdentificationStrategy
body: <p>I am migrating my multitenant application from Webapi to aspnet core. In webapi version I was using <code>TenantIdentificationStrategy</code> that identified tenants based on request path on <code>HttpContext</code>.</p>
  
  <p>Moving to aspnet core, I am able to wire-up autofac successfully. I am not able to figure out how to wireup the tenant strategy. I tried injecting <code>IHttpContextAccessor</code> in <code>ConfigureServices</code> as </p>
  
  <pre><code>services.AddSingleton&lt;IHttpContextAccessor, HttpContextAccessor&gt;(); 
  </code></pre>
  
  <p>and my strategy looks like this</p>
  
  <pre><code>public class AssetClassIdentificationStrategy: ITenantIdentificationStrategy {
      private readonly IHttpContextAccessor _accessor;
      public AssetClassIdentificationStrategy(IHttpContextAccessor httpContextAccessor)
      {
          _accessor = httpContextAccessor;
      }
      public bool TryIdentifyTenant(out object tenantId) {
          tenantId = null;
          var context = _accessor.HttpContext;
          if (context != null &amp;&amp; context.Request != null )){
              var matchRegex = new Regex(@"\/[\d,\.,\w]*\/(\w*)\/.*");
              var match = matchRegex.Match(context.Request.Path.ToString());
              if (match.Success) {
                  tenantId = match.Groups[1].Value.ToLower();
              }
          }
          return tenantId != null;
      }
  }
  </code></pre>
  
  <p>What I am seeing is that <code>HttpContextAccessor</code> is being injected correctly, where as <code>HttpContext</code> within it always null. As a result of this none of the multitenant services are being resolved.</p>
  
  <p>Looked around for samples, but couldn't find anything that fits the problem. There used to be a <code>RequestParameterTenantIdentificationStrategy</code> in Autofacv3 which is no longer supported. Appreciate any help with this.</p>
  
  <p><strong>Edit</strong>
  Fixed issue with code and adding Startup.cs as requested. </p>
  
  <pre><code>public class Startup
  {
      public Startup(IHostingEnvironment env) {
          var builder = new ConfigurationBuilder()
              .SetBasePath(env.ContentRootPath)
              .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
  
          Configuration = builder.Build();
      }
  
      public IConfigurationRoot Configuration { get; }
  
      public IServiceProvider ConfigureServices(IServiceCollection services)
      {
          services.Configure&lt;CacheConfig&gt;(Configuration.GetSection("Caching"),false);
          services.AddMvc();
          services.AddSingleton&lt;IHttpContextAccessor, HttpContextAccessor&gt;();
          services.AddTransient&lt;ITenantIdentificationStrategy,AssetClassIdentificationStrategy&gt;();
  
          var builder = new ContainerBuilder();
          builder.Populate(services);
          builder.RegisterType&lt;TenantInfo&gt;().WithProperty("TenantName", "unknown").As&lt;ITenantInfo&gt;();
  
          var container = builder.Build();
  
          ITenantIdentificationStrategy tenantIdentificationStrategy;
          bool isMultiTenant = container.TryResolve(out tenantIdentificationStrategy);
  
          var mtc = new MultitenantContainer(tenantIdentificationStrategy, container);
          mtc.ConfigureTenant("pesonalLoans", b =&gt; {
              b.RegisterType&lt;TenantInfo&gt;().WithProperty("TenantName","pesonalLoans") .As&lt;ITenantInfo&gt;();
          });
          mtc.ConfigureTenant("retirement", b =&gt; {
              b.RegisterType&lt;TenantInfo&gt;().WithProperty("TenantName", "retirement").As&lt;ITenantInfo&gt;();
          });
  
          return mtc.Resolve&lt;IServiceProvider&gt;();
  
      }
  
      public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
      {
          LoggingConfig.Register(Configuration, loggerFactory);
          app.UseMvc();
      }
  }
  
  
  public class ValuesController : Controller {
      private ITenantInfo _tenant;
      public ValuesController(ITenantInfo tenant) {
          _tenant = tenant;
      }
  
      [HttpGet]
      public string Get()
      {
          return  _tenant.TenantName;
      }
  }
  
  
  public interface ITenantInfo {
      string TenantName { get; set; }
  }
  public class TenantInfo: ITenantInfo
  {
      public string TenantName { get; set; }
  }
  </code></pre>
  
  <p><strong>Edit 3</strong> project.json</p>
  
  <pre><code>{
    "dependencies": {
      "Microsoft.NETCore.App": {
        "version": "1.0.0-rc2-3002702",
        "type": "platform"
      },
      "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
      "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
      "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
      "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
      "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-rc2-final",
      "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
      "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
      "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
      "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
      "Autofac": "4.0.0-rc2-240",
      "Autofac.Multitenant": "4.0.0-beta8-219",
      "System.IdentityModel.Tokens.Jwt": "5.0.0-rc2-305061149",
      "Autofac.Extensions.DependencyInjection": "4.0.0-rc2-240",
      "System.Reflection": "4.1.0-rc2-24027",
      "System.Reflection.Primitives": "4.0.1-rc2-24027",
      "System.Reflection.Extensions": "4.0.1-rc2-24027",
      "System.Reflection.TypeExtensions": "4.1.0-rc2-24027",
      "System.Reflection.Emit": "4.0.1-rc2-24027",
      "System.Reflection.Context": "4.0.1-rc2-24027",
      "System.Reflection.DispatchProxy": "4.0.1-rc2-24027",
      "System.Reflection.Emit.ILGeneration": "4.0.1-rc2-24027",
      "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
      "Microsoft.AspNet.Mvc.Formatters.Xml": "6.0.0-rc1-final",
      "Microsoft.AspNet.Mvc.Formatters.Json": "6.0.0-rc1-final",
    },
  
    "tools": {
      "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
        "version": "1.0.0-preview1-final",
        "imports": "portable-net45+win8+dnxcore50"
      }
    },
  
    "frameworks": {
      "netcoreapp1.0": {
        "imports": [
          "dotnet5.6",
          "dnxcore50",
          "portable-net45+win8"
        ]
      }
    },
  
    "buildOptions": {
      "emitEntryPoint": true,
      "preserveCompilationContext": true
    },
  
    "runtimeOptions": {
      "gcServer": true
    },
  
    "scripts": {
      "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
    }
  }
  </code></pre>