简体   繁体   English

由于 System.Runtime 错误,无法将 Blazor 应用程序迁移到 .Net 5

[英]Cannot Migrate Blazor App to .Net 5 Due to System.Runtime Error

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.我一直在通过 Udemy 中的一个很棒的教程来了解有关 Blazor 的更多信息(https://www.udemy.com/course/programming-in-blazor-aspnet-core/ ),但遇到了一个绊脚石不完全确定该怎么做。

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.从 .Net Standard 2.1 升级到 .Net 5 时,我在尝试运行此示例 Blazor 应用程序时遇到此错误(因此它不会遇到我的任何代码): 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')我看到此 SO 链接存在类似问题,但它并没有真正让我对 go 关闭。

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.使用.Net 的早期版本,您安装了最新版本,然后 Visual Studio 选择了它,您切换了项目,然后就离开了 - 一切都是无缝的,并且可以正常工作。 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.不过,对于一些较新的东西,微软的消息传递非常混乱,我现在遇到的问题是在 Udemy 教程中,我需要利用IJSObjectReference接口来做一些事情。 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:当我第一次将其添加到代码中时,无法解析类型引用,因此快速搜索指出我需要通过更改以下内容将项目移动到.Net 5:

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

to this (because Visual Studio doesn't always show.Net 5 as an option):对此(因为 Visual Studio 并不总是显示.Net 5 作为选项):

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

Seemed simple enough, so I changed the Client Blazor project to this and tried to compile.看起来很简单,所以我把Client Blazor项目改成这个并尝试编译。 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)这给了我这个错误: Project BlazorMovies.Client is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Project BlazorMovies.Client supports: net5.0 (.NETCoreApp,Version=v5.0) Project BlazorMovies.Client is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Project BlazorMovies.Client supports: net5.0 (.NETCoreApp,Version=v5.0) . 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') .我想好了,接下来我将服务器提升到 5.0,然后一切都编译正常,但是一旦我把它拉起来,我就会收到这个错误: 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.然后我记得不记得我是否安装了.Net 5,所以我去检查并(通过docs.microsoft.com )我只安装了 4.8.03752。 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.然后,我进行了一些搜索以尝试找到 .Net 安装程序,并且有多个(请参见此处) - 甚至页面的布局也确实令人难以抗拒,大约 20 个安装链接分散在各处。 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.我知道我至少需要 x64,所以我首先安装了 SDK,因为它表示支持 Visual Studio,而且速度比我预期的要快得多(基于之前安装的.Net),但现在 VS 正在展示.Net 5,这似乎很有希望,我重新检查了注册表。 and it still says 4.8,03752 and when I went to Add/Remove programs.当我去添加/删除程序时,它仍然显示 4.8,03752。 .Net 5 doesn't show up like all the other versions, I next installed the Hosting Bundle which said it was successful. .Net 5 并没有像所有其他版本一样出现,我接下来安装了 Hosting Bundle,它说它是成功的。 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.我知道 Blazor 是相当新的,但是微软在.Net Framework、.Net Standard、.Net Core 之间的消息非常混乱,现在迁移回.Net 5 似乎需要多个安装程序,我真的不知道 go 在哪里下一个。 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.根据堆栈跟踪,该错误完全是从 Web 汇编代码中生成的,因此它似乎与我正在做的事情无关。 Here's a screenshot of everything Chrome shows me in the console:这是 Chrome 在控制台中向我显示的所有内容的屏幕截图: 堆栈跟踪

To migrate Blazor Webassembly Application from netstandard2.1 to .NET 5 , you could refer the following steps:要将 Blazor Webassembly 应用程序从netstandard2.1迁移到.NET 5 ,您可以参考以下步骤:

  1. Install or update Visual Studio 2019 to version 16.8.0+, and install the .NET 5 SDK . 安装或更新Visual Studio 2019 到版本 16.8.0+,并安装.NET 5 SDK

  2. Change the Blazor Webassembly project setting.更改 Blazor Webassembly 项目设置。

    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:创建一个 Blazor Webassembly 项目(创建应用程序时, select .net core 3.1 ,然后,TargetFrameWork 将是.netstandard2.1文件:1 应用程序。

    • Update the SDK from Microsoft.NET.Sdk.Web to Microsoft.NET.Sdk.BlazorWebAssembly将 SDK 从Microsoft.NET.Sdk.Web更新到Microsoft.NET.Sdk.BlazorWebAssembly
    • Set the TargetFramework to net5.0将 TargetFramework 设置为net5.0
    • In case you have it, remove the package reference to Microsoft.AspNetCore.Components.WebAssembly.Build .如果你有它,请删除 package 对Microsoft.AspNetCore.Components.WebAssembly.Build的引用。

    After updating, the.csproj file content as below:更新后.csproj文件内容如下:

     <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.更新 Nuget 依赖项版本。

    The result like this:结果是这样的:

    在此处输入图像描述

    Then, the final.csproj file content looks as below:然后,最终的.csproj 文件内容如下所示:

     <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.然后运行应用程序,网站运行良好。

    在此处输入图像描述

Have you changed the header node in the *.csproj too?您是否也更改了 *.csproj 中的 header 节点?

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

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

  ...

</Project>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法加载文件或程序集System.Runtime - Cannot load file or assembly System.Runtime .NET WinForms:无法加载 System.Runtime - .NET WinForms: Cound not load System.Runtime .NET Framework:如何更新 System.Runtime? - .NET Framework: How to update System.Runtime? 在.net 4.6.2中使用.net标准2.0库找不到System.Runtime 4.1.0.0 - System.Runtime 4.1.0.0 cannot be found using .net standard 2.0 lib in .net 4.6.2 .NET Core 3.0 迁移错误 IAsyncEnumerable<t> System.Interactive.Async 和 System.Runtime 中都存在</t> - .NET Core 3.0 migration error IAsyncEnumerable<T> exists in both System.Interactive.Async and System.Runtime 使用 CSharpCodeProvider 时获取运行时错误程序集“System.Runtime” - getting runtime error assembly 'System.Runtime' while using CSharpCodeProvider 初始化XAML预览器时找不到System.Runtime程序集 - Cannot find System.Runtime assembly while initializing XAML Previewer ASP.NET Core 2.0站点-FileIOException System.Runtime - ASP.NET Core 2.0 Site - FileIOException System.Runtime C# .NET“无法加载文件或程序集 System.Runtime” - C# .NET "Could not load file or assembly System.Runtime" 无法使用.Net Core dll加载文件或程序集&#39;System.Runtime&#39; - Could not load file or assembly 'System.Runtime' with .Net Core dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM