.NET Core RuntimeIdentifier与TargetFramework

Ale*_*zis 16 .net-core asp.net-core asp.net-core-webapi visual-studio-2017 runtime-identifier

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

<TargetFramework>netstandard1.6</TargetFramework>
<RuntimeIdentifier>win7</RuntimeIdentifier>
Run Code Online (Sandbox Code Playgroud)

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

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

UPDATE1

我有web api和引用的.net核心库.从VS2015迁移的所有东西.现在我有根项目 <TargetFrameworks>netcoreapp1.1;net461</TargetFrameworks>.当我通过VS2017发布时出现错误:

C:\ Program Files\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 .但是现在我甚至无法构建app,得到错误:

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.我做错了什么?

UPDATE2

我有8个项目正在解决.其中3个是xUnit测试.因此我们有5个项目.这5个中的4个是类库,1个是我的web-app.所有4个类库都有:

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

我的网络应用:

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

如何发布我的网络应用程序?

Tse*_*eng 17

<TargetFramework>(或<TargetFrameworks>当你想有多个目标,如net451,一个或多个netstandard1.x等).每个<TargetFramework>/ <TargetFrameworks>条目将创建一组程序集并位于其中bin\Debug\<targetframeworkid>.

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

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

<RuntimeIdentifier>/ <RuntimeIdentifiers>另一方面主要用于NuGet.它告诉NuGet您需要哪些包.例如,如果您想要定位Linux,Mac和Windows,某些程序集需要本机库(例如加密.在Windows上将使用CryptoAPI,但在Linux和Mac上则需要OpenSSL).这包括非托管dll和*.so(Linux)文件.

ie <RuntimeIdentifiers>win7-x64;win-7x86;ubuntu.16.10-x64</RuntimeIdentifiers>将为win7(x64和x86)版本生成nuget恢复包,仅为ubuntu生成x64.这是必需的,因为当您在Windows上工作时,您还需要下载这些本机库,以便部署/打包它们dotnet publish.

这里有一个小抓虽然:当你有一个完整的.NET框架引用<TargetFramework><TargetFrameworks>,则必须指定一个<RuntimeIdentifier>(单数,不是复数<RuntimeIdentifiers>),否则你会得到一个错误.

例如:

<PropertyGroup>
    <TargetFrameworks>netstandard1.0;net451</TargetFrameworks>
    <RuntimeIdentifiers>win7-x64;win-7x86;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>
Run Code Online (Sandbox Code Playgroud)

  • `&lt;TargetFrameworks&gt;netstandard1.0;net451&lt;/TargetFrameworks&gt;` 在他使用 .Net Standard 时是不必要的。您正在使用许多过时的做法 (3认同)
  • 不幸的是@J.Doe,在很多地方,netstandard 本身是不够的——例如,如果您引用 System.Drawing.Common 库,那么(截至 2018 年 11 月)您必须交叉编译分别针对 net462(或更高版本)和 netstandard。 (2认同)

J. *_*Doe 8

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

更多内容请参阅文档

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

<PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
    <RuntimeIdentifier>win7-x64;ubuntu.16.10-x64</RuntimeIdentifier>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

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

  • 不应该是运行时标识符吗?RuntimeIdentifier 只允许一个ID。 (3认同)