-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
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
-
Create a new Blazor App / Blazor Server App project
-
Create a new Class
public class MyTesingClass {}
-
Configure DI for HTTP context and the created class
services.AddHttpContextAccessor(); services.AddScoped<MyTesingClass>();
-
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>
-
Run the project - the result is
false
-
Change
render-mode="ServerPrerendered"
in (_Host.cshtml
) torender-mode="Static"
The result istrue
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: b377529961Runtime 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