title: ASP.Net Core dependency injection | How to access user or route data upon creation
body: <h1>Scenario</h1>
  
  <p>I am working to follow the IoC pattern and use the Microsoft DI Framework, but I'm hitting a wall and can't decide if it's my mindset, my approach, or I'm just doing it wrong. </p>
  
  <p>I have a multi-tenant application that utilizes a Utility class to handle isolation and accessing the data of the tenant based on their unique configuration. The tenant is identified during authentication, but the tenant data has to be accessible and handled based on the request. This Utility class is registered under ConfigureServices in the <code>Startup.cs</code> and the constructor requires two parameters  - a <code>TenantDbContext</code> and a <code>Tenant</code>.</p>
  
  <pre><code>public class TenantUtility{
       public TenantUtility(TenantDbContext context, Tenant tenant){/*...*/}
  }
  </code></pre>
  
  <h1>Problem</h1>
  
  <p>Realizing that I'm probably pushing the limits of the DI Framework, I'm trying to build an implementationFactory in the <code>Startup.cs</code>. I've tried two approaches for accessing the tenant Id to build the <code>Tenant</code> object: one is using a User Claim, the second is a Route parameter. </p>
  
  <pre><code>services.AddTransient&lt;TenantUtility&gt;((svc)=&gt; {
     var tenantContext = svc.GetService&lt;TenantDbContext&gt;();
     var accessor = svc.GetService&lt;IHttpContextAccessor&gt;();
     var httpContext = accessor.HttpContext;
  
      //httpContext is NULL...
      //How do I get access to the tenant?
      Common.Tenant t = new Common.Tenant();
      //Set Tenant Identifier in t (once I get it)
      return new StudentDataManager(tenantContext, t);
  });
  </code></pre>
  
  <p>In both situations, when I setup the ImplementationFactory inside the Startup.cs, I don't have access to the user and I don't have access to the RouteData (or can't figure out how to get it) -- I even tried using <code>IHttpContextAccessor</code>, but the <code>HttpContext</code> property is null. </p>
  
  <p>Am I approaching the pattern incorrectly? Should I be able to use DI for this level of detail be injected into the Utility before the Utility class is passed into the Controller?</p>