title: Injected dependency into automapper profile in .net core - IHttpContextAccessor returns null
body: <p><strong>Goal:</strong> To inject <code>IHttpContextAccessor</code> as a dependency into autmapper profile 's constructor</p>
  
  <p><strong>Why</strong>: I need to pass in the current user 's identity 's name as part of construction of one of my domain objects</p>
  
  <p><strong>Issue</strong>
  The obtained User identity is always null</p>
  
  <p>What do I have so far?</p>
  
  <p><strong>Startup.cs</strong></p>
  
  <pre><code>mc.AddProfile(new DtoToOtherProfile(new HttpContextAccessor()));
  
  //OR
  
  var mapperConfiguration = new MapperConfiguration(mc =&gt;
          {
              IServiceProvider provider = services.BuildServiceProvider();
              mc.AddProfile(new DtoToOtherProfile(provider.GetService&lt;IHttpContextAccessor&gt;()));
          });
  </code></pre>
  
  <p><strong>DtoToOtherProfile.cs</strong></p>
  
  <pre><code> public DtoToOtherProfile(IHttpContextAccessor httpContextAccessor)
      {
       _httpContextAccessor = httpContextAccessor;
  
       CreateMap&lt;MyDto, MyOther&gt;()
      .ConstructUsing(myDto =&gt; new myOther(myDto.prop1, myDto .prop2,  _httpContextAccessor.HttpContext.User.Identity.Name)); // the name is what i intend to pass
        // other mapping
      }
  </code></pre>
  
  <p><strong>Controller.cs</strong></p>
  
  <pre><code>  public async Task&lt;IActionResult&gt; Create([FromBody]MyDto model)
      {
          if (!ModelState.IsValid)
          {
              return BadRequest(ModelState);
          }
  
          // save campaign
          var myOtherModel = _mapper.Map&lt;MyOther&gt;(model);
          // myOtherModel.UserName is always null 
         // rest
        }
  </code></pre>
  
  <p>Is there something am missing? Pointers please?</p>