Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazer Web App Scoped Service Instantiated Twice (pre-rendering disabled) #53804

Closed
1 task done
mrlife opened this issue Feb 4, 2024 · 5 comments
Closed
1 task done
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved

Comments

@mrlife
Copy link
Contributor

mrlife commented Feb 4, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

A scoped service is instantiated twice in an app with an interactive server render mode and no pre-rendering.

In my project, a razor component sets a value in the scoped service after the scoped service is instantiated the first time, but then it is instantiated again, causing the value to be lost.

Expected Behavior

A scoped service should be instantiated once when pre-rendering is disabled.

Steps To Reproduce

Please see the linked project. With a breakpoint in the TestService constructor, notice the breakpoint is hit twice due to the injection of the service in _Imports.razor.

https://github.com/mrlife/BlazorServiceInstantiation

Exceptions (if any)

No response

.NET Version

8.0.101

Anything else?

.NET SDK:
Version: 8.0.101
Commit: 6eceda187b
Workload version: 8.0.100-manifests.69afb982

Runtime Environment:
OS Name: Mac OS X
OS Version: 14.3
OS Platform: Darwin
RID: osx-arm64
Base Path: /usr/local/share/dotnet/sdk/8.0.101/

.NET workloads installed:
Workload version: 8.0.100-manifests.69afb982
[maui]
Installation Source: SDK 8.0.100
Manifest Version: 8.0.3/8.0.100
Manifest Path: /usr/local/share/dotnet/sdk-manifests/8.0.100/microsoft.net.sdk.maui/8.0.3/WorkloadManifest.json
Install Type: FileBased

Host:
Version: 8.0.1
Architecture: arm64
Commit: bf5e279d92

.NET SDKs installed:
7.0.311 [/usr/local/share/dotnet/sdk]
7.0.312 [/usr/local/share/dotnet/sdk]
8.0.100 [/usr/local/share/dotnet/sdk]
8.0.101 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 7.0.14 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.15 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 7.0.14 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.15 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
None

Environment variables:
Not set

global.json file:
Not found

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Feb 4, 2024
@davidalexandru11
Copy link

Hello mrlife

I have the same problem...

Every time I use QuickGrid in my razor component it is initialized 2 times.

My itemsprovider is used in the following way:

`itemsProviderTestModel1 = async request =>
{
await Task.Delay(200);
var result = await TestRepo.GetTestModelListGrid(request, 50, filteredName, "", filteredAge);
CountTestModel1 = result.Items.Count;

return result;

};`

I tried different methods to avoid this, but without success...

But I noticed that the problem persists only when pagination or virtualisation is used.

I also discovered that when I use pagination and I want, for example, to press a button that, for example, opens a modal, itemsprovider it is fired even though the button is outside the Quickgrid component.

@mrlife
Copy link
Contributor Author

mrlife commented Feb 4, 2024

@davidalexandru11, do you have pre-rendering disabled at the app level?

@davidalexandru11
Copy link

mrlife Yes

@javiercn javiercn added area-blazor Includes: Blazor, Razor Components and removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels Feb 7, 2024
@javiercn
Copy link
Member

javiercn commented Feb 7, 2024

@mrlife thanks for contacting us.

This is by design. You have the @inject directive applied in _Imports.razor, which will inject it in all components on the folder hierachy, this includes the App.razor component (which always renders statically) and well as your pages

@bytes00134
Copy link

bytes00134 commented Mar 20, 2024

Hi,

i have the same problem @davidalexandru11 -> pagination redners twice.
But i have no idea how to solve this.

This is by design. You have the @Inject directive applied in _Imports.razor, which will inject it in all components on the folder hierachy, this includes the App.razor component (which always renders statically) and well as your pages

Is there any example here for help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved
Projects
None yet
Development

No branches or pull requests

5 participants
@mrlife @javiercn @davidalexandru11 @bytes00134 and others