C
C#•2mo 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•2mo ago
possibly pre-rendering
Grault
GraultOP•2mo ago
Shouldn't that have been prevented by the render mode?
Anu6is
Anu6is•2mo ago
no all modes are still prerendered you can disable with something like new InteractiveWebassembly(false) false being prerender
Grault
GraultOP•2mo ago
Where would that constructor be placed? I don't think I have anything like that.
Anu6is
Anu6is•2mo ago
where you define the mode currently
Salman
Salman•2mo ago
are you trying to use that clientside component in a server component ?
Anu6is
Anu6is•2mo ago
@rendermode @(new InteractiveWebassembly(prerender: false))
Grault
GraultOP•2mo 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•2mo ago
don't believe so
Grault
GraultOP•2mo ago
Ah, got it.
@rendermode @(new InteractiveWebAssemblyRenderMode(prerender: false))
@rendermode @(new InteractiveWebAssemblyRenderMode(prerender: false))
Anu6is
Anu6is•2mo ago
Grault
GraultOP•2mo 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