【问题标题】:.NET Core RuntimeIdentifier vs TargetFramework.NET Core RuntimeIdentifier 与 TargetFramework
【发布时间】:2017-04-20 12:34:12
【问题描述】:

有人可以在csproj文件(VS2017)中解释这两个的目的吗:

<TargetFramework>netstandard1.6</TargetFramework>
<RuntimeIdentifier>win7</RuntimeIdentifier>

我刚刚从 VS2015 迁移,现在无法发布我的 web api,因为看起来我应该只使用一个目标框架。此外,我不能指定多个 RID。 所有这些改变的事情让我感到沮丧。没有什么是从头开始的,应该一遍又一遍地克服一些事情。

我只想在 Windows 上开发我的 web-api,在这里运行 xUnit 测试,然后部署 web-api 以在 linux (ubuntu) 服务器上运行。 我应该在 csproj 的两个参数中输入什么? 非常感谢具有良好解释的链接。

更新1

我有带有引用的 .net 核心库的 web api。从 VS2015 迁移的所有内容。现在在根项目中我有 &lt;TargetFrameworks&gt;netcoreapp1.1;net461&lt;/TargetFrameworks&gt;。当我通过 VS2017 发布时出现错误:

C:\程序 文件\dotnet\sdk\1.0.3\Sdks\Microsoft.NET.Sdk\buildCrossTargeting\Microsoft.NET.Sdk.targets(31,5): 错误:不指定不支持“发布”目标 目标框架。当前项目针对多个框架, 请指定已发布应用程序的框架。

但我在发布中指定目标框架为netcoreapp1.1。 好的。然后我用<PropertyGroup Condition="$(TargetFramework)'=='netcoreapp1.1'"> <RuntimeIdentifier>ubuntu.16.10-x64</RuntimeIdentifier> </PropertyGroup> 更新了我的csproj,如下所示。 但是现在我什至无法构建应用程序,得到错误:

5>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.Sdk.targets(92,5): 错误:资产文件 '\obj\project.assets.json' 没有 有一个 '.NETCoreApp,Version=v1.1/ubuntu.16.10-x64' 的目标。确保 您已经为 TargetFramework='netcoreapp1.1' 恢复了这个项目,并且 RuntimeIdentifier='ubuntu.16.10-x64'。

我只想在 windows 8.1/windows7 上使用 VS2017 开发并部署到 ubuntu 16.10。 我做错了什么?

更新2

我有 8 个项目正在解决中。其中 3 个是 xUnit 测试。因此,我们有 5 个项目。这 5 个中的 4 个是类库,1 个是我的网络应用程序。 所有 4 个类库都有这个:

<TargetFrameworks>netstandard1.6;net461</TargetFrameworks>    
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
    <Reference Include="System" />
    <Reference Include="Microsoft.CSharp" />
</ItemGroup>

我的网络应用:

<TargetFrameworks>netcoreapp1.1;net461</TargetFrameworks>
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
    <Reference Include="System" />
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>

如何发布我的网络应用?

【问题讨论】:

    标签: asp.net-core .net-core visual-studio-2017 asp.net-core-webapi runtime-identifier


    【解决方案1】:

    &lt;TargetFramework&gt;(或&lt;TargetFrameworks&gt;,当您想要多个目标时,例如net451、一个或多个netstandard1.x 等)。根据 &lt;TargetFramework&gt; / &lt;TargetFrameworks&gt; 条目,将创建一组程序集并位于 bin\Debug\&lt;targetframeworkid&gt;) 内。

    当您想在 .NET Core 中使用不同的库时(因为您使用的库仅适用于完整的 .NET Framework,例如 4.5.1)或从 .NET Core 中删除此功能(因为它不受支持),这很有用.

    它用于构建和 NuGet 还原。即,您不能在 .NET Core 项目中仅使用 net451 库(例如 netstandard 1.1 - 但您可以在 net451 项目中使用 netstandard1.1)。

    另一边的&lt;RuntimeIdentifier&gt;/&lt;RuntimeIdentifiers&gt;主要用于NuGet。它告诉 NuGet 你需要哪些包。例如,如果您想针对 Linux、Mac 和 Windows,某些程序集需要本地库(例如加密。在 Windows 上将使用 CryptoAPI,但在 Linux 和 Mac 上您需要 OpenSSL)。这包括非托管 dll 和 *.so (Linux) 文件。

    &lt;RuntimeIdentifiers&gt;win7-x64;win7-x86;ubuntu.16.10-x64&lt;/RuntimeIdentifiers&gt; 将为 win7(x64 和 x86)版本和 x64 仅针对 ubuntu 制作 nuget 恢复包。这是必需的,因为当您在 Windows 上工作时,您也需要下载这些本机库,以便使用 dotnet publish 部署/打包它们。

    这里有个小问题:当您在 &lt;TargetFramework&gt;&lt;TargetFrameworks&gt; 中有完整的 .NET Framework 引用时,您必须指定单个 &lt;RuntimeIdentifier&gt;(单数,而不是复数 &lt;RuntimeIdentifiers&gt;),否则您将得到一个错误。

    例如:

    <PropertyGroup>
        <TargetFrameworks>netstandard1.0;net451</TargetFrameworks>
        <RuntimeIdentifiers>win7-x64;win7-x86;ubuntu.16.10-x64</RuntimeIdentifiers>    
    </PropertyGroup>
    
    <!-- This entry will only be used for the .NET Framework 4.5.1 output -->
    <PropertyGroup Condition="'$(TargetFramework)' == 'net451'">
        <RuntimeIdentifier>win7-x64</RuntimeIdentifier>
    </PropertyGroup>
    

    【讨论】:

    • &lt;TargetFrameworks&gt;netstandard1.0;net451&lt;/TargetFrameworks&gt; 在使用 .Net Standard 时是不必要的。您正在使用许多过时的做法
    • 我试过了,但它对我不起作用。查看我对这个问题的更新。
    • @Alezis:阅读消息 ;) 当您有多个目标时,您必须告诉 dotnet publish 要发布哪个版本。 docs.microsoft.com/en-us/dotnet/articles/core/tools/…,即dotnet publish --runtime ubuntu.16.10-x64。同样在 netstandard1.0;net451 中,net451 已过时(因为 netstandard1.0 在 .NET 4.5 及更高版本上运行。但如果您使用仅在 1.3 (.NET 4.6) 中可用的功能,则 netstandard1.3;net451 可用于创建两个构建. 见docs.microsoft.com/en-us/dotnet/articles/standard/library
    • @Tseng 感谢您的帮助,但它也不起作用。我运行dotnet publish --runtime ubuntu.16.10-x64 并得到同样的错误。同样正如我所说,我的根应用程序(网络应用程序)针对netcoreapp1.1;net461。在我的所有 5 个项目中,我不使用 net451 仅 3 个项目 netcoreapp1.1 net461 netstandard1.6
    • 不幸的是@J.Doe 有很多地方仅靠 netstandard 是不够的——例如,如果您引用 System.Drawing.Common 库,那么(截至 11 月, 2018)您必须分别交叉编译目标 net462(或更高版本)和 netstandard。
    【解决方案2】:

    RID 是 Runtime IDentifier 的缩写。 RID 用于识别目标 应用程序或资产(即程序集)所在的操作系统 会跑。它们看起来像这样:“ubuntu.14.04-x64”、“win7-x64”、 “osx.10.11-x64”。对于具有本机依赖项的包,它将 指定可以在哪些平台上恢复包。

    更多内容请关注docs

    首先将正确的 RID 从 win7 更改为 win7-x64win7-x86。接下来添加其他 RID,如 ubuntu。例如:

    <PropertyGroup>
        <TargetFramework>netstandard1.6</TargetFramework>
        <RuntimeIdentifier>win7-x64;ubuntu.16.10-x64</RuntimeIdentifier>
    </PropertyGroup>
    

    目标框架看起来不错。更多阅读docs

    【讨论】:

      猜你喜欢
      • 2018-07-10
      • 2020-01-26
      • 2020-06-21
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      相关资源
      最近更新 更多