Blazor .Net 9 Default template simple usage of a service - porting from an earlier version

Kasrak 46 Reputation points
2025-01-16T18:50:15.5666667+00:00

I tried to port this simple project:

https://github.com/JWealthall/BlazorWindowResize

to .net 9.0

So, I created a simple sample at:

https://github.com/KKacer/BlazorTestWindowSize

I'm trying to understand the usage and changes I need to do in the 2 Program.cs files (one in the Client project, and the other in the Server project)

What I did was placing all the Interfaces, Services and Models inside the Client project.

But using the same Program.cs which was implemented in that project may not work here:

using BlazorWindowResize;

using BlazorWindowResize.Interfaces;

using BlazorWindowResize.Services;

using Microsoft.AspNetCore.Components.Web;

using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

var builder = WebAssemblyHostBuilder.CreateDefault(args);

builder.RootComponents.Add<App>("#app");

builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

builder.Services.AddScoped<IBrowserService, BrowserService>();

var host = builder.Build();

await host.Services.GetRequiredService<IBrowserService>().Init();

await host.RunAsync();

Should I add part of these implementations in the Server project? What changes are needed and why?

Thanks in advance for any help.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,656 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 70,376 Reputation points
    2025-01-16T22:04:50.71+00:00

    you will need to add the service in both projects. The server project will need the service for server interactive or client static pre-render. The client project when used as WASM will also need the service.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Kasrak 46 Reputation points
    2025-01-17T09:08:26.5+00:00

    Thanks for the hint,

    with some edits, I was able to figure it out.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.