title: AddScoped service is not retaining settings throughout the request
body: <p>I have created an Interface </p>
  
  <pre><code>public interface ICurrentUser
  {
      Task&lt;bool&gt; Set(UserAuth user);
      User Get();
  }
  </code></pre>
  
  <p>and a class </p>
  
  <pre><code>public class CurrentUserSvc : Interface.ICurrentUser
  {
      private User _u;
      private UserAuth _ua;
      private AppDbContext db;
  
      public CurrentUserSvc(AppDbContext db) {
          this.db = db;
      }
  
      public User Get()
      { 
          return _u;
      }
  
      public async Task&lt;bool&gt; Set(UserAuth ua)
      {
          _ua = ua; // this is the default EntityFramework IdentityUser
          _u = await db.AppUsers // this is my applicaiton's 'extra settings'
                                 // user used to ensure passowrd fields are
                                 // not passed about everywhere
              .Where(u =&gt; u.UserID == _ua.UserID)
              .SingleAsync();
          return true;
      }
  }
  </code></pre>
  
  <p>In <code>Startup.cs</code> I set </p>
  
  <pre><code>services.AddScoped&lt;ICurrentUser, CurrentUserSvc&gt;();
  // I also add a service which will be used later in a scoped
  // lifecycle (though I've also tried transient on that one)
  services.AddScoped&lt;IProductDbSvc, ProductDbSvc&gt;();
  </code></pre>
  
  <p>Later I call to a piece of middleware: </p>
  
  <pre><code>    public async Task&lt;Task&gt; Invoke(HttpContext hc)
      {
          if (hc.User.Identity.IsAuthenticated) {
              UserAuth iu = await _um.FindByIdAsync(hc.User.GetUserId());
              await _cus.Set(iu);
          }
          // the values are definitely set correctly here.
          // I have inspected them during debug
          return _next(hc);
      }
  </code></pre>
  
  <p>Later still I try to access the content of the <code>CurrentUserSvc</code> I try to access the current user via the GET </p>
  
  <pre><code>    public ProductDbSvc(AppDbContext db, ICurrentUser cu){
          this.db = db;
          this.cu = cu;
          // the values in cu are NULL here. Get() returns null
          this.CurrentUser = cu.Get();
      }
  </code></pre>
  
  <p>but the result of Get() is <code>null</code> I was expecting that a Scoped param would retain the values set earlier in the request lifecycle.</p>
  
  <p>What am I missing? Is there some other way to ensure the scoped-singleton retains the user data throughout the application's lifecycle.</p>
  
  <p><strong>UPDATE</strong>:  I've created a generic project that illustrates this problem generically. <a href="https://github.com/AlexChesser/AspnetIdentitySample" rel="nofollow">https://github.com/AlexChesser/AspnetIdentitySample</a></p>
  
  <ul>
  <li>check out the repo</li>
  <li>build and run in visualstudio or DNX</li>
  <li>register a local user</li>
  <li>try to view the service on <a href="http://localhost:5000/api/currentuser" rel="nofollow">http://localhost:5000/api/currentuser</a></li>
  </ul>
  
  <p>You'll notice that within the DEBUG output you can see that the correct user details are set, but within the actual controller itself the values returned are null.</p>
  
  <p><strong>UPDATE 2</strong> the working sample is on this branch in github <a href="https://github.com/AlexChesser/AspnetIdentitySample/tree/dependencyinjectionscoped" rel="nofollow">https://github.com/AlexChesser/AspnetIdentitySample/tree/dependencyinjectionscoped</a></p>
  
  <p><strong>UPDATE 3</strong> turns out scoped parameters can be injected into the INVOKE method of custom middleware as well. <a href="https://github.com/AlexChesser/AspnetIdentitySample/commit/25b010a5ae45678c137b2ad05c53ccd659a29101" rel="nofollow">https://github.com/AlexChesser/AspnetIdentitySample/commit/25b010a5ae45678c137b2ad05c53ccd659a29101</a> altering the invoke method will allow for scoped parameters to be injected correctly.</p>
  
  <pre><code>public async Task Invoke(HttpContext httpContext,
      ICurrentUserService cus,
      UserManager&lt;ApplicationUser&gt; um)
  {
      if (httpContext.User.Identity.IsAuthenticated)
      {
          ApplicationUser au = await um.FindByIdAsync(httpContext.User.GetUserId());
          await cus.Set(au);
      }
      await _next(httpContext);
  }
  </code></pre>
  
  <p><strong>UPDATE 4</strong> - I discovered an issue with my middleware signature last night which is pretty important.  Code above has been edited to the correct form. Specifically the method was <code>Task&lt;Task&gt;</code> and <code>return _next(...)</code></p>
  
  <p>This was resulting in a "whitescreen" death on certain page loads (async called badly will not throw a stack trace)</p>
  
  <p>By altering to a <code>Task</code> and using <code>await next(...)</code> the code functions properly and eliminates the intermittent whitescreen death caused by badly implemented async in dotnet5.</p>