Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.
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

Description

@jlVidal

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions