dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.36k stars 9.99k forks source link

In Blazor Web App, getting console message about class added to Servi #51994

Closed peterhvogel closed 11 months ago

peterhvogel commented 11 months ago

Is there an existing issue for this?

Describe the bug

I've created a solution using the Blazor Web App template with the Interactivity Type set to Auto and the Interactivity location set to Global. In the BlazorApp1 project's program.cs file, I add a server-side component using this code:

builder.Services.AddTransient<IWarehouseRepo, WarehouseRepo>();

In a Blazor component in the BlazorApp1.Client project, I retrieve the component using this code:

@inject IWarehouseRepo whRepo;

Two or three seconds after the app displays, at the bottom of the component's page, I getthe message "An unhandled error has occured. Reload", I can, by the way, use the component in, for example, my OnInitializedAsync method, so everything seems to be working. However, using/not using the component has no effect on whether the error message appears.

When I go to the console log, I find this message at the top:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Cannot provide a value for property 'whRepo' on type 'BlazorApp1.Client.Pages.Home'. There is no registered service of type 'ProductManagement.Repository.IWarehouseRepo'. System.InvalidOperationException: Cannot provide a value for property 'whRepo' on type 'BlazorApp1.Client.Pages.Home'. There is no registered service of type 'ProductManagement.Repository.IWarehouseRepo'. at Microsoft.AspNetCore.Components.ComponentFactory.<>c__DisplayClass9_0.g__Initialize|1(IServiceProvider serviceProvider, IComponent component)

Expected Behavior

No error message, two-way databinding continues to work.

Steps To Reproduce

  1. Create a solution in Visual Studio 2022 Preview using the Blazor Web App template, taking the default names, setting the Interactivity Type to Auto and the Interactivity location to Global
  2. Add a component to the services collection in the Blazor
  3. Use @inject to retrieve the component in a Blazor component

Exceptions (if any)

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Cannot provide a value for property 'whRepo' on type 'BlazorApp1.Client.Pages.Home'. There is no registered service of type 'ProductManagement.Repository.IWarehouseRepo'. System.InvalidOperationException: Cannot provide a value for property 'whRepo' on type 'BlazorApp1.Client.Pages.Home'. There is no registered service of type 'ProductManagement.Repository.IWarehouseRepo'. at Microsoft.AspNetCore.Components.ComponentFactory.<>c__DisplayClass9_0.g__Initialize|1(IServiceProvider serviceProvider, IComponent component)

Subsequently, two-way databinding in EditForm stops working -- if I enter a new value in the InputText element, the value in the WarehouseName property in wh field is not updated:

<EditForm Model="@wh" OnSubmit="@HandleSubmit" FormName="Home">

 <InputText @bind-Value="wh.WarehouseName" /> <br />
 <button type="submit">Submit</button>

.NET Version

8.0.100-rc.2.2.23502.2

Anything else?

.NET SDK: Version: 8.0.100-rc.2.23502.2 Commit: 0abacfc2b6

Runtime Environment: OS Name: Windows OS Version: 10.0.22000 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\8.0.100-rc.2.23502.2\

.NET workloads installed: There are no installed workloads to display.

Host: Version: 8.0.0-rc.2.23479.6 Architecture: x64 Commit: 0b25e38ad3

.NET SDKs installed: 8.0.100-rc.2.23502.2 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 8.0.0-rc.2.23480.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 8.0.0-rc.2.23479.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 8.0.0-rc.2.23479.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables: Not set

global.json file: Not found

Learn more: https://aka.ms/dotnet/info

Download .NET: https://aka.ms/dotnet/download

davidfowl commented 11 months ago

The stack trace is showing that the webassembly part of your code is what is complaining. The service needs to be added there too…

peterhvogel commented 11 months ago

The secret to long-term success: Understanding the error messages.

Thanks!

Yours truly,

Peter Vogel, MBA My mama died and left me My papa died and left me I ain’t good looking, baby But I’m somewhat sweet and kind

@.***

From: David Fowler @.> Sent: Sunday, November 12, 2023 11:43 AM To: dotnet/aspnetcore @.> Cc: Peter Vogel @.>; Author @.> Subject: Re: [dotnet/aspnetcore] In Blazor Web App, getting console message about class added to Servi (Issue #51994)

The stack trace is showing that the webassembly part of your code is what is complaining. The service needs to be added there too…

— Reply to this email directly, view it on GitHubhttps://github.com/dotnet/aspnetcore/issues/51994#issuecomment-1807179441, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AJLG2LXY5C5YQMVE5LJ6S6DYED4BVAVCNFSM6AAAAAA7HM5UHKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMBXGE3TSNBUGE. You are receiving this because you authored the thread.Message ID: @.**@.>>

javiercn commented 11 months ago

@peterhvogel thanks for contacting us.

Your service needs to be registered both on the Server and on the Client projects if its going to be used by a component running in webassembly.

peterhvogel commented 11 months ago

Thank you so much for this. My only feedback: I'd already been given this solution. Nothing wrong with getting it twice, though (and, as I said before, the real secret to successful programming: reading and understanding the error messages).


From: Javier Calvarro Nelson @.> Sent: Friday, November 24, 2023 8:52 AM To: dotnet/aspnetcore @.> Cc: Peter Vogel @.>; Mention @.> Subject: Re: [dotnet/aspnetcore] In Blazor Web App, getting console message about class added to Servi (Issue #51994)

@peterhvogelhttps://github.com/peterhvogel thanks for contacting us.

Your service needs to be registered both on the Server and on the Client projects if its going to be used by a component running in webassembly.

— Reply to this email directly, view it on GitHubhttps://github.com/dotnet/aspnetcore/issues/51994#issuecomment-1825707288, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AJLG2LVLLECHWF7T5PEIOI3YGCRBHAVCNFSM6AAAAAA7HM5UHKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMRVG4YDOMRYHA. You are receiving this because you were mentioned.Message ID: @.***>

ghost commented 11 months ago

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.