how to use scoped dependency in a singleton in C# / ASP
<p>I'm new to C#/ASP coming from a Java world. I've read this article: <a href="https://docs.asp.net/en/latest/fundamentals/dependency-injection.html#service-lifetimes-and-registration-options" rel="nofollow noreferrer">https://docs.asp.net/en/latest/fundamentals/dependency-injection.html#service-lifetimes-and-registration-options</a> which wisely warns about the dangers associated with injecting a dependency with a smaller scope. Unfortunately it does not explain how to solve this issue in C#/ASP.
In Java there's a concept of Provider</p>

<pre><code>interface Provider&lt;T&gt; { T get(); }
</code></pre>

<p>which, among other things helps to solve the scoping issue:
whenever a binding for some type T is register we can inject an automatically generated instance of Provider&lt;T> instead of T and then get an instance of T whenever it is needed: an automatically generated Provider makes sure that we get an instance appropriate for the current scope (whatever this scope is: HTTP request, HTTP session or other custom scopes). The standard DI framework built into ASP.NET core does not have anything like this, but I thought in C# it should be very easy to implement as C# generics don't suck like java's do (<a href="https://docs.oracle.com/javase/tutorial/java/generics/erasure.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/tutorial/java/generics/erasure.html</a>). So I've created the following class:</p>

<pre><code>public class Provider&lt;T&gt;: IProvider&lt;T&gt; {
  private readonly IServiceProvider serviceProvider;
  public Provider(IServiceProvider serviceProvider) {
    this.serviceProvider = serviceProvider;
  }
  public T IProvider&lt;T&gt;.Get() {
    return serviceProvider.GetService&lt;T&gt;();
  }
}
</code></pre>

<p>and I attemtped to use it the following way:</p>

<pre><code>public class SingletonService : ISingletonService {
  private readonly IProvider&lt;IScopedService&gt; scopedServiceProvider;

  public SingletonService(IProvider&lt;IScopedService&gt; scopedServiceProvider) {
    this.scopedServiceProvider = scopedServiceProvider;
  }

  public string PerformMyTask() {
    var scopedDependency = scopedServiceProvider.Get();
    // do something with scopedDependency to verify we get instances
    // appropriate for the current scope
  }
}
</code></pre>

<p>and in my Startup class:</p>

<pre><code>public void ConfigureServices(IServiceCollection services) {
  services.AddSingleton&lt;ISingletonService, SingletonService&gt;();
  services.AddScoped&lt;IScopedService, ScopedService&gt;();
  services.AddTransient&lt;IProvider&lt;IScopedService&gt;, Provider&lt;IScopedService&gt;&gt;();
  // other bindings here
}
</code></pre>

<p>Unfortunately this does not work the way I intended as IServiceProvider instance seems to be also scoped to the current HTTP request and I get exactly the same instance of ScopedDependency from my provider during processing of different requests :(</p>

<p>Any hints how can I solve this problem?
Is there any "higher level" object than ServiceProvider maybe, bound roughly to application lifecycle (not to the current request) that creates instances of request scoped objects (or of ServiceProvider itself) that I can inject into my Provider objects instead of ServiceProvider? For example in Java if I use google Guice as a DI framework there is an Injector object, usually created at the startup of an application which holds all the type bindings and has a method</p>

<pre><code>&lt;T&gt; T getInstance(Class&lt;T&gt; type);
</code></pre>

<p>which checks what is the current scope and returns a corresponding instance.</p>

<p><strong>edit:</strong></p>

<p>I think that one possible way to do it would be to get a new reference to instance of ServiceProvider each time in the Proivder&lt;T>.Get() method instead of injecting in the constructor and storing as an instance var. This way my components would still not be polluted with a reference to the framework specific IServiceProvider as it would be hidden from them in the implementation of Provider&lt;T> that they access via the abstract IProvider&lt;T> interface.  I can't however find on the web if it's possible to get such a reference from my Provider class and how to do this. Any pointers in this direction would be appreciated :)</p>

<p>Thanks!</p>