Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

IApplicationBuilder.ApplicationServices instance, returns different scoped services/objects from 'HttpContext.RequestServices instance #697

Closed
jlVidal opened this issue Aug 31, 2016 · 3 comments
Milestone

Comments

@jlVidal
Copy link

jlVidal commented Aug 31, 2016

Suppose that I registered a scoped service.

services.AddScoped<IMyService, MyService>();

Now, consider the following scenario... On startup class:

app.Map("/test", level2 =>
{
    level2.Use(
                    a =>
                    {
                        return (b =>
                        {
                                var instance = context.RequestServices.GetService<IMyService>();
                                DoSomething(instance);

                            return a.Invoke(b);
                        });
                    });
    });

     level2.UseStaticFiles(new StaticFileOptions
                   {
                       FileProvider = new MyFileProvider(level2.ApplicationServices);
                   });
}

Consider that I'm trying to use IMyService implementation inside of MyFileProvider, but that would happen just when a request comes in. During the same request as the "Use" method, the MyService instance is being created again (same request/same thread, just accessed by IServiceProvider (from IApplicationBuilder.ApplicationServices) instead of IServiceProvider (from HttpContext.RequestServices).

Anyway, since the service was defined scoped. Is that supposed to be a normal behavior?

@jlVidal jlVidal changed the title IApplicationBuilder.ApplicationServices instance, returns different Scoped services/objects from 'HttpContext.RequestServices instance IApplicationBuilder.ApplicationServices instance, returns different scoped services/objects from 'HttpContext.RequestServices instance Aug 31, 2016
@muratg
Copy link

muratg commented Sep 7, 2016

I believe this is the current design. Services taken from ApplicationServices is not scoped. Does it break any common scenario for you?

@muratg muratg added this to the Discussions milestone Sep 7, 2016
@khellang
Copy link
Contributor

khellang commented Sep 8, 2016

Actually, the service resolved from ApplicationServices ends up being a singleton, because the container is never disposed (before app shutdown). This is called a captive dependency and will throw an exception in v1.1. See aspnet/DependencyInjection#430

A scoped instance is always resolved from the provider you're asking, and disposed when the same provider is disposed. There's no shared, scoped instances between providers.

@Eilon
Copy link
Contributor

Eilon commented Jun 9, 2017

We are closing this issue because no further action is planned for this issue. If you still have any issues or questions, please log a new issue with any additional details that you have.

@Eilon Eilon closed this as completed Jun 9, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants