简体   繁体   English

Blazor 添加一个 ASP.NET Core 时出现 Webassembly 错误 Web API 项目参考

[英]Blazor Webassembly error when adding a ASP.NET Core Web API Project Reference

I created two fresh new projects a Blazor Web Assembly without checking the option of ASP.NET Core hosted because I did not want to create the other two projects that get created.我创建了两个全新的项目 Blazor Web Assembly 没有选中ASP.NET Core hosted选项,因为我不想创建其他两个已创建的项目。 Followed by a fresh new ASP.NET Core Web API project.接下来是全新的 ASP.NET Core Web API 项目。 After creating this project and writing any code, I added a few nuget packages to my web api project that I plan on using.创建此项目并编写任何代码后,我将一些 nuget 包添加到我计划使用的 web api 项目中。 I then expanded the Blazor Web Assembly project and right clicked on Dependencies, clicked on Add Project Reference and selected my Web API project.然后我展开 Blazor Web Assembly 项目并右键单击 Dependencies,单击 Add Project Reference 并选择我的 Web API 项目。 Then when building both projects, the web api passes and the blazor web assembly fails because of Error.NETSDK1082 .然后在构建这两个项目时,web api 通过,blazor web 程序集由于Error.NETSDK1082而失败。 The exact error is shown below.确切的错误如下所示。

C:\Program Files\do.net\sdk\6.0.100-preview.3.21202.5\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.FrameworkReferenceResolution.targets(391,5): error.NETSDK1082: There was no runtime pack for Microsoft.AspNetCore.App available for the specified RuntimeIdentifier 'browser-wasm'. C:\Program Files\do.net\sdk\6.0.100-preview.3.21202.5\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.FrameworkReferenceResolution.targets(391,5): error.NETSDK1082 :没有适用于指定 RuntimeIdentifier“browser-wasm”的 Microsoft.AspNetCore.App 运行时包。

I looked up online for that error and attempted the solution where I add two lines of code into my.csproj file inbetween the <PropertyGroup> section, but even after adding this it failed.我在网上查找了该错误并尝试了解决方案,我在<PropertyGroup>部分之间的 my.csproj 文件中添加了两行代码,但即使添加了它也失败了。 I even deleted the obj folder and rebuilt the blazor project but it produced the same error.我什至删除了 obj 文件夹并重建了 blazor 项目,但它产生了同样的错误。

<RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
<UseBlazorWebAssembly>true</UseBlazorWebAssembly>

Blazor Web Assembly Project : Blazor Web Assembly Project

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

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

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.10" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.10" PrivateAssets="all" />
    <PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\PolkWebAPI\PolkWebAPI.csproj" />
  </ItemGroup>

</Project>

ASP.NET Core Web API Project : ASP.NET Core Web API Project

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

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

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.10" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.10">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.2" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
  </ItemGroup>

</Project>

I have not added any code to any of the projects I just installed a few nuget packages to the web api project and added the project reference to the blazor web assembly which produced the error.我没有向任何项目添加任何代码我只是将几个 nuget 包安装到 web api 项目并将项目引用添加到产生错误的 blazor web 程序集。

Following up to request :跟进要求

in the project.assets.json file I see two other sections that include browser-wasm :project.assets.json文件中,我看到另外两个包含browser-wasm

 "downloadDependencies": [
      {
        "name": "Microsoft.NETCore.App.Runtime.browser-wasm",
        "version": "[5.0.4, 5.0.4]"
      }
    ],
    "frameworkReferences": {
      "Microsoft.NETCore.App": {
        "privateAssets": "all"
      }
    },
    "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\6.0.100-preview.3.21202.5\\RuntimeIdentifierGraph.json"
  }
},
"runtimes": {
  "browser-wasm": {
    "#import": []
  }
}

Which section are you talking about to uninstall each nuget package manually?你说的是哪个部分要手动卸载每个 nuget package?

Update: It seems you were trying to add a reference to the Web API to the blazor web assembly (WA) project.更新:您似乎试图将对 Web API 的引用添加到 blazor web 程序集 (WA) 项目中。 You do not need to do that.你不需要那样做。 There are two WA models:有两种 WA 模型:

  1. .net core hosted: You can create this type of project by ticking the ".net core" hosted when creating your WA project. .net core hosted:创建WA项目时勾选“.net core”hosted即可创建此类项目。 It creates three projects by default.它默认创建三个项目。 Client, server (mostly API controllers) and shared.客户端、服务器(多为API控制器)共享。 Client and server both refer to the shared project.客户端和服务器都引用共享项目。 The shared project has all the classes that both WA application and controllers in server.共享项目包含服务器中 WA 应用程序和控制器的所有类。
  2. Standalone: In this case, you get two projects.独立:在这种情况下,您将获得两个项目。 Note that you can add another shared project like in (1) above to use classes that both can use.请注意,您可以像上面 (1) 中那样添加另一个共享项目,以使用两者都可以使用的类。 However, in this case the API controller need not even know it is talking to a blazor app.然而,在这种情况下,API controller 甚至不需要知道它正在与 blazor 应用对话。

You see the 'browser-wasm' error because of nuget packages that are incompatible with blazor wasm.由于 nuget 软件包与 blazor wasm 不兼容,您会看到“浏览器 wasm”错误。 You got the error because you added the server project reference to the WA project.您收到错误是因为您将服务器项目引用添加到了 WA 项目。

Read more here 在这里阅读更多

If cleanly opting for (1) or (2) does not work, search for project.assets.json in your project folder and search the section with browser-wasm.如果完全选择 (1) 或 (2) 不起作用,请在项目文件夹中搜索project.assets.json并使用 browser-wasm 搜索该部分。 Check the list of packages in that section and try uninstalling those nuget packages one by one.检查该部分中的软件包列表,然后尝试将 nuget 个软件包一一卸载。

https://github.com/do.net/as.netcore/issues/36711 https://github.com/do.net/as.netcore/issues/36711

暂无
暂无

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

相关问题 从 Blazor WebAssembly (WASM) 项目调用外国 API(**不是** ASP.Net Core,托管) - Call an foreign API from a Blazor WebAssembly (WASM) Project (**NOT** ASP.Net Core, hosted) ASP.NET 核心 Web API - 项目参考中的 ConnectionString - ASP.NET Core Web API - ConnectionString in project reference 托管在 ASP.NET Core 项目上的 Blazor Webassembly 项目中的身份验证问题 - Problem with Authentification in Blazor Webassembly project which hosted on ASP.NET Core project Blazor Webassembly 项目、ASP.NET Core 3.1 中不会出现断点, - Breakpoints won't hit in Blazor Webassembly project, ASP.NET Core 3.1, 部署托管到 Azure 的 Blazor WebAssembly 应用 ASP.NET Core - Deploy Blazor WebAssembly App ASP.NET Core hosted to Azure 使用 Blazor Webassembly 和 ASP.NET 内核安全文件下载 - Secure File Download Using Blazor Webassembly and ASP.NET Core 如何使用 ASP.NET 核心托管发布 Blazor WebAssembly - How to publish Blazor WebAssembly with ASP.NET Core hosted Blazor WebAssembly 应用程序与个人帐户和 ASP.NET 核心托管 - 令牌请求 - “错误”:“unauthorized_client” - Blazor WebAssembly App with Individual Accounts and ASP.NET Core Hosted - Token request - "error": "unauthorized_client" 尝试运行ASP.NET Core Web API项目时出现错误NU1605 - Error NU1605 when trying to run ASP.NET Core web API project 在 asp .Net Core 项目中添加 wsdl web 引用 - Adding wsdl web reference in asp .Net Core project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM