aspnetcore: Error when shipping blazor 0.8 razor component project to .NET Core 3.0 Preview 3. Detail: 'IServiceCollectoin' does not contain a definition for 'AddRazorComponents'. 'IApplicationBuilder' does not contain a definition for 'UseRazorComponents'
My server-side blazor app was working in blazor 0.8. But after I upgraded it to blazor 0.9, i got a few errors
'IServiceCollection' does not contain a definition for and no accessible extension method accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?
'IApplicationBuilder' does not contain a definition for and no accessible extension method accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
What i did:
- Upgraded SDK to 3.0.100-preview3-010431
- Updated the Blazor packages and .NET CLI tool references to 0.9.0-preview3-19154-02.
- Updated the remaining Microsoft.AspNetCore.* packages to 3.0.0-preview3-19153-02.
- Fixed the JSRuntime.
server.csproj
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Blazor.Server" Version="0.9.0-preview3-19154-02" />
<PackageReference Include="Microsoft.AspNetCore.Components.Server" Version="3.0.0-preview3-19153-02" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0-preview3-19153-02" />
</ItemGroup>
App.csproj
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="0.9.0-preview3-19154-02" />
<PackageReference Include="Microsoft.AspNetCore.Components.Build" Version="3.0.0-preview3-19153-02" />
</ItemGroup>
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 3
- Comments: 18 (2 by maintainers)
This is my scenario:
After follow all instructions, I get the error of this issue when I try to run app as server-side (razor components).
I dig into new generated app from template 0.9.0 code and I see
indexis now on/Pagesfolder and other significant changes.Please, someone can post real instructions to switch between client side and razor components on 0.9.0?
One clarification: You can still setup an app so that it can be switched between the client and server hosting models, but it requires a bit of code. For details, see Steve’s comment here: https://github.com/aspnet/AspNetCore/issues/8523#issuecomment-477563907.
OK, so I got my solution converted from 0.8.0 using Blazer (WebAssembly) to 0.9.0 using Razor Components (Serverside Blazor).
First, I had to move all my code from my Server project (where the controllers were and some other helper classes) and move it to the Client project.
Then I needed to take what I had for my Server Startup.cs and Program.cs and use that instead of the one that was in Client.
Any services that you had defined in your Client Startup.cs file will need to be moved to the Server Startup.cs file.
I tried following all the steps here as best as I could: https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-core-3-0-preview-3/
Then, when that didn’t work as expected, I saw this post.
I added the following to ConfigureServices in Startup.cs
services.AddRazorComponents();I then added the following to Configure in Startup.cs
I had an issue modifying the Nuget Packages, so I opened up the .csproj file and added what I needed manually.
Also, make sure that your TagetFramework is:
<TargetFramework>netcoreapp3.0</TargetFramework>I have a folder structure as follows (which seems to be important):
/Components - this folder houses all the razor components and everyting has been renamed to have the .razor file extension Components/Pages Components/Shared /Controllers /Pages
I had also run into an issue when calling StateHasChanged where it said something about not being on the UI thread or something like that (sorry can’t remember exactly what the wording was), I had to call it like this instead: base.Invoke(StateHasChanged);
There were a bunch of other issues I ran into, but these were the ones that were the hardest for me to fix. Also, a lot of the other issues were really just organization and renaming of namespaces and that.
I hope this helps someone else. Took me three days to figure these out and I think I’ve got some grey hair now.