1
votes

I have been working through an awesome tutorial within Udemy to learn more about Blazor (https://www.udemy.com/course/programming-in-blazor-aspnet-core/), but have hit a stumbling block that I'm not entirely sure what to do with.

Short Version

When upgrading to .Net 5 from .Net Standard 2.1, I end up with this error when trying to run this sample Blazor application as soon as it loads up (so it's not hitting any of my code): System.TypeLoadException: Could not resolve type with token 01000014 from typeref (expected class 'System.Threading.Tasks.Task' in assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') I see a similar problem with this SO link, but it didn't really give me much to go off of.

Detailed Version

With prior versions of .Net, you installed the latest, then Visual Studio picked that up, you switched projects and away you went - everything was seamless and just worked. With some of the newer stuff though, Microsoft's messaging has been extremely confusing and the problem I'm hitting now is inside that Udemy tutorial I need to utilize the IJSObjectReference interface to do something. When I first added that to the code, the type reference couldn't be resolved so a quick search pointed me to needing to move the project to .Net 5 by changing this:

<PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>

to this (because Visual Studio doesn't always show .Net 5 as an option):

<PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

Seemed simple enough, so I changed the Client Blazor project to this and tried to compile. That gives me this error: Project BlazorMovies.Client is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Project BlazorMovies.Client supports: net5.0 (.NETCoreApp,Version=v5.0). I figured okay, I'll bump Server to 5.0 next and then everything compiles fine, but as soon as I pull it up, I get this error: System.TypeLoadException: Could not resolve type with token 01000014 from typeref (expected class 'System.Threading.Tasks.Task' in assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'). Then I remembered not recalling if I'd installed .Net 5 yet, so I went to check and (via docs.microsoft.com) I only have 4.8.03752 installed. I then did some searches to try and find the .Net installers and there were multiple (see here) - even the layout of the page is really overwhelming, with ~20 install links scattered throughout. I knew I needed at least x64, so I first installed the SDK since it said Visual Studio support and that went significantly faster than I expected (based on prior installs of .Net), but now VS is showing .Net 5 which seemed promising! I re-checked the registry though, and it still says 4.8.03752 and when I went to Add/Remove programs, .Net 5 doesn't show up like all the other versions. I next installed the Hosting Bundle which said it was successful, but the sample app still has the exact same error.

Any advice? I know Blazor is quite new, but with Microsoft's extremely confusing messaging between .Net Framework, .Net Standard, .Net Core and now a migration back into .Net 5 that seems to need multiple installers, I don't really know where to go next. That error is entirely generated from within the Web Assembly code according to the stack trace, so it doesn't appear to be anything related to what I'm doing. Here's a screenshot of everything Chrome shows me in the console: Stack Trace

2

2 Answers

3
votes

To migrate Blazor Webassembly Application from netstandard2.1 to .NET 5, you could refer the following steps:

  1. Install or update Visual Studio 2019 to version 16.8.0+, and install the .NET 5 SDK.

  2. Change the Blazor Webassembly project setting.

    Create a Blazor Webassembly project (When create the application, select .net core 3.1, then, the TargetFrameWork will be .netstandard2.1), Open the application .csproj file:

    • Update the SDK from Microsoft.NET.Sdk.Web to Microsoft.NET.Sdk.BlazorWebAssembly
    • Set the TargetFramework to net5.0
    • In case you have it, remove the package reference to Microsoft.AspNetCore.Components.WebAssembly.Build.

    After updating, the .csproj file content as below:

     <Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly"> 
       <PropertyGroup>
         <TargetFramework>net5.0</TargetFramework> 
       </PropertyGroup> 
       <ItemGroup>
         <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" /> 
         <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.1" PrivateAssets="all" />
         <PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
       </ItemGroup> 
     </Project>
    
  3. Update the Nuget dependencies version.

    The result like this:

    enter image description here

    Then, the final .csproj file content looks as below:

     <Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly"> 
       <PropertyGroup>
         <TargetFramework>net5.0</TargetFramework> 
       </PropertyGroup> 
       <ItemGroup>
         <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.2" /> 
         <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.2" PrivateAssets="all" />
         <PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
       </ItemGroup> 
     </Project>
    
  4. Clean the entire solution, otherwise the build engine won’t be able to re-generate all the required files with the updated framework.

    Then running the application, the website works well.

    enter image description here

1
votes

Have you changed the header node in the *.csproj too?

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  ...

</Project>