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

Blazor bypasses running client Program.cs and client side DI fails #55648

Closed
sam-wheat opened this issue May 9, 2024 · 3 comments
Closed

Blazor bypasses running client Program.cs and client side DI fails #55648

sam-wheat opened this issue May 9, 2024 · 3 comments
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved

Comments

@sam-wheat
Copy link

The attached app is a default Blazor template for an app with authentication.

I have made the following changes:

Server Project

App.razor:

private IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account")
	   ? null
	   : new InteractiveWebAssemblyRenderMode(prerender: false);	

Client project

Added a class called MyObject.
Registered a MyObject singleton in Program.cs.
Added a control called Shared/LoginStatusControl.razor.
Inject MyObject in Home.razor and LoginStatusControl.
Add LoginStatusControl to MainLayout.
Add @attribute [Authorize] to Home.razor.

Problem:

In the configuration described it appears that Blazor bypasses running Program.cs. This causes MyObject to not be registered as a singleton and ultimately results in this error:

Cannot provide a value for property 'myObject' on type 'BlazorAuthExample2.Client.Shared.LoginStatusControl'. There is no registered service of type 'BlazorAuthExample2.Client.MyObject'.

Removing @attribute [Authorize] from Home.razor appears to mitigate this issue however in my production app, all pages including "/" need to direct the user to login if they have not done so.

Also, I need to use new InteractiveWebAssemblyRenderMode(prerender: false) because I have some controls that render modals and they do not render correctly if I don't use that render mode.

Question:
How can I protect all pages with @attribute [Authorize] and still have my client side DI work?

BlazorAuthExample2.zip

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label May 9, 2024
@javiercn
Copy link
Member

@sam-wheat thanks for contacting us.

If you are using web assembly, you need to register your object in both environments.

@sam-wheat
Copy link
Author

sam-wheat commented May 10, 2024

@javiercn as a singleton in both environments? How many instances will I have?

@javiercn
Copy link
Member

@sam-wheat You'll have an instance on the server and an instance on the client.

You have two processes, one running server-side and one running client-side. They are completely independent and don't automatically share any kind of state.

@mkArtakMSFT mkArtakMSFT closed this as not planned Won't fix, can't repro, duplicate, stale May 14, 2024
@mkArtakMSFT mkArtakMSFT added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label May 14, 2024
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. Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants