Skip to content

Rendering a component: An injected scopeded instance differs from the one from RequestServices  #20570

@TPIvan

Description

@TPIvan

Describe the bug

When I configure a service for being injected with scoped lifetime (.AddScoped), the instance which is injected to a component field is different from the instance which can be obtained by calling HttpContext.RequestServices.GetService. The problem occurs in blazor-server (services.AddServerSideBlazor();) when rendering a component (render-mode="Server" or second pass when render-mode="ServerPrerendered"). When the component is prerendered (render-mode="Static" or first pass when render-mode="ServerPrerendered") the instaces are the same (as expected).

To Reproduce

  1. Create a new Blazor App / Blazor Server App project

  2. Create a new Class

      public class MyTesingClass {}
    
  3. Configure DI for HTTP context and the created class

      services.AddHttpContextAccessor(); 
      services.AddScoped<MyTesingClass>(); 
    
  4. Put the following code to Index.razor

     @inject Microsoft.AspNetCore.Http.IHttpContextAccessor contextAccessor
     @inject MyTesingClass  myInstance;
     <div>
       Instances are the same: @(myInstance == contextAccessor.HttpContext.RequestServices.GetService(typeof(MyTesingClass)))
     </div>
    
  5. Run the project - the result is false

  6. Change render-mode="ServerPrerendered" in (_Host.cshtml) to render-mode="Static"
    The result is true

There is also SO question: Blazor: HttpContext.RequestServices provides a different scoped instance than IServiceProvider

Further technical details

  • ASP.NET Core version

.NET Core SDK (reflecting any global.json):
Version: 3.1.101
Commit: b377529961

Runtime Environment:
OS Name: Windows
OS Version: 10.0.18362
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.1.101\

Host (useful for support):
Version: 3.1.1
Commit: a1388f194c

.NET Core SDKs installed:
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.509 [C:\Program Files\dotnet\sdk]
2.2.103 [C:\Program Files\dotnet\sdk]
3.1.101 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
BlazorApp11.zip

  • The IDE VS 2019 16.4.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions