C
C#•3d ago
Grault

Blazor WASM: Injecting a service

I'm trying to inject a service to extract details (dimensions, so far) of the browser. The code for the service is from https://blazor.tips/blazor-how-to-ready-window-dimensions/ . The service is registered in the Client project's Program.cs:
C#
builder.Services.AddScoped<BrowserService>();
C#
builder.Services.AddScoped<BrowserService>();
Obviously the service needs to be entirely on the client, so the component which consumes it has:
C#
@rendermode InteractiveWebAssembly
@inject BrowserService Browser
C#
@rendermode InteractiveWebAssembly
@inject BrowserService Browser
When I run the project and open the page which contains the component, I receive: >InvalidOperationException: Cannot provide a value for property 'Browser' on type 'Quilt.Client.Components.Sheet'. There is no registered service of type 'Quilt.Client.Utilities.BrowserService'. Is some of the component running on the server? Why?
12 Replies
Anu6is
Anu6is•3d ago
possibly pre-rendering
Grault
GraultOP•3d ago
Shouldn't that have been prevented by the render mode?
Anu6is
Anu6is•3d ago
no all modes are still prerendered you can disable with something like new InteractiveWebassembly(false) false being prerender
Grault
GraultOP•3d ago
Where would that constructor be placed? I don't think I have anything like that.
Anu6is
Anu6is•3d ago
where you define the mode currently
Salman
Salman•3d ago
are you trying to use that clientside component in a server component ?
Anu6is
Anu6is•3d ago
@rendermode @(new InteractiveWebassembly(prerender: false))
Grault
GraultOP•3d ago
Got it, thanks. The Sheet component is on a page in the Client project. Do I need a @using for that? Blazor components break intellisense...
Anu6is
Anu6is•3d ago
don't believe so
Grault
GraultOP•3d ago
Ah, got it.
@rendermode @(new InteractiveWebAssemblyRenderMode(prerender: false))
@rendermode @(new InteractiveWebAssemblyRenderMode(prerender: false))
Anu6is
Anu6is•3d ago
Grault
GraultOP•3d ago
🤞 Alright, thanks. I got rid of the error, now I need to figure out why nothing is displaying.
Want results from more Discord servers?
Add your server