简体   繁体   English

.NET Core RuntimeIdentifier 与 TargetFramework

[英].NET Core RuntimeIdentifier vs TargetFramework

Can someone explain the purpose of this two in csproj file (VS2017):有人可以在csproj文件(VS2017)中解释这两个的目的:

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

I just migrated from VS2015 and now can't publish my web api because it looks I should use only one target framework.我刚刚从 VS2015 迁移,现在无法发布我的 web api,因为看起来我应该只使用一个目标框架。 In addition I can't specify multiple RIDs.此外,我无法指定多个 RID。 All these changed things make me frustrated.所有这些改变的事情让我感到沮丧。 Nothing works from scratch, should overcome something over and over.没有什么是从头开始的,应该一遍又一遍地克服某些东西。

I just want developing my web-api on windows, run xUnit tests here and then deploy web-api to run on linux (ubuntu) server.我只想在 Windows 上开发我的 web-api,在这里运行 xUnit 测试,然后部署 web-api 以在 linux (ubuntu) 服务器上运行。 What I should put in both parameters in csproj ?我应该在 csproj 的两个参数中放入什么? Links with good explanation is highly appreciated.具有良好解释的链接受到高度赞赏。

Update1更新1

I have web api with referenced .net core libraries.我有带有引用的 .net 核心库的 web api。 Everything where migrated from VS2015.从 VS2015 迁移的所有内容。 Now in root project I have <TargetFrameworks>netcoreapp1.1;net461</TargetFrameworks> .现在在根项目中我有<TargetFrameworks>netcoreapp1.1;net461</TargetFrameworks> When I publish via VS2017 I got error:当我通过 VS2017 发布时,出现错误:

C:\\Program Files\\dotnet\\sdk\\1.0.3\\Sdks\\Microsoft.NET.Sdk\\buildCrossTargeting\\Microsoft.NET.Sdk.targets(31,5): error : The 'Publish' target is not supported without specifying a target framework. C:\\Program Files\\dotnet\\sdk\\1.0.3\\Sdks\\Microsoft.NET.Sdk\\buildCrossTargeting\\Microsoft.NET.Sdk.targets(31,5): error : The 'Publish' target is not supported without specified a目标框架。 The current project targets multiple frameworks, please specify the framework for the published application.当前项目针对多个框架,请指定发布的应用程序的框架。

But I have specified target framework in publish as netcoreapp1.1 .但是我在发布netcoreapp1.1目标框架指定为netcoreapp1.1 OK.好的。 Then I updated my csproj with <PropertyGroup Condition="$(TargetFramework)'=='netcoreapp1.1'"> <RuntimeIdentifier>ubuntu.16.10-x64</RuntimeIdentifier> </PropertyGroup> as suggested below.然后我用<PropertyGroup Condition="$(TargetFramework)'=='netcoreapp1.1'"> <RuntimeIdentifier>ubuntu.16.10-x64</RuntimeIdentifier> </PropertyGroup>更新了我的 csproj,如下所示。 But now I even can't build app, get error:但是现在我什至无法构建应用程序,出现错误:

5>C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\MSBuild\\Sdks\\Microsoft.NET.Sdk\\build\\Microsoft.NET.Sdk.targets(92,5): error : Assets file '\\obj\\project.assets.json' doesn't have a target for '.NETCoreApp,Version=v1.1/ubuntu.16.10-x64'. 5>C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\MSBuild\\Sdks\\Microsoft.NET.Sdk\\build\\Microsoft.NET.Sdk.targets(92,5): error : Assets file '\\ obj\\project.assets.json' 没有 '.NETCoreApp,Version=v1.1/ubuntu.16.10-x64' 的目标。 Ensure you have restored this project for TargetFramework='netcoreapp1.1' and RuntimeIdentifier='ubuntu.16.10-x64'.确保您已为 TargetFramework='netcoreapp1.1' 和 RuntimeIdentifier='ubuntu.16.10-x64' 恢复此项目。

I just want develop with VS2017 at windows 8.1/windows7 and deploy to ubuntu 16.10.我只想在 windows 8.1/windows7 上使用 VS2017 进行开发并部署到 ubuntu 16.10。 What I'm doing wrong ?我做错了什么?

Update2更新2

I have 8 projects in solution.我有 8 个解决方案。 3 of them are xUnit tests.其中 3 个是 xUnit 测试。 Thus we have 5 projects.因此我们有5个项目。 4 of these 5 are class libraries and 1 is my web-app.这 5 个中有 4 个是类库,1 个是我的网络应用程序。 All 4 class libraries have this:所有 4 个类库都有这个:

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

My web app:我的网络应用程序:

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

How to publish my web-app ?如何发布我的网络应用程序?

The <TargetFramework> (or <TargetFrameworks> when you want have multiple targets, such as net451 , one or multiple netstandard1.x etc). <TargetFramework> (或<TargetFrameworks>当您想要多个目标时,例如net451 、一个或多个netstandard1.x等)。 Per <TargetFramework> / <TargetFrameworks> entry one set of assemblies will be created and located inside bin\\Debug\\<targetframeworkid> ).每个<TargetFramework> / <TargetFrameworks>条目将创建一组程序集并位于bin\\Debug\\<targetframeworkid> )。

This is useful, when you want to use a different library in .NET Core (because the library you used only works with full .NET Framework such as 4.5.1) or remove this feature from ie .NET Core because it's unsupported.当您想在 .NET Core 中使用不同的库(因为您使用的库仅适用于完整的 .NET Framework,例如 4.5.1)或从 ie .NET Core 中删除此功能时,这很有用,因为它不受支持。

It is used for both, building and NuGet restore.它用于构建和 NuGet 还原。 ie you can't use a net451 only library in a .NET Core project ( netstandard 1.1 for example - but you can use netstandard1.1 in a net451 project).即您不能在 .NET Core 项目中使用仅net451库(例如netstandard 1.1 - 但您可以在net451项目中使用netstandard1.1 )。

<RuntimeIdentifier> / <RuntimeIdentifiers> on the other side is used for NuGet mainly.另一端的<RuntimeIdentifier> / <RuntimeIdentifiers>主要用于 NuGet。 It tells NuGet which packages you need.它告诉 NuGet 你需要哪些包。 For example if you want to target Linux, Mac and Windows, certain assemblies require native libraries (such as encryption. On windows CryptoAPI will be used, but on Linux and Mac you need OpenSSL).例如,如果您想针对 Linux、Mac 和 Windows,某些程序集需要本机库(例如加密。在 Windows 上将使用 CryptoAPI,但在 Linux 和 Mac 上您需要 OpenSSL)。 This includes non-managed dlls and *.so (Linux) files.这包括非托管 dll 和 *.so (Linux) 文件。

ie <RuntimeIdentifiers>win7-x64;win7-x86;ubuntu.16.10-x64</RuntimeIdentifiers> will make nuget restore packages for win7 (both x64 and x86) versions and x64 only for ubuntu.<RuntimeIdentifiers>win7-x64;win7-x86;ubuntu.16.10-x64</RuntimeIdentifiers>将为 win7(x64 和 x86)版本和 x64 仅为 ubuntu 制作 nuget 恢复包。 This is required, because when you work on windows you need to download these native libraries too so you deploy/package them with dotnet publish .这是必需的,因为当您在 Windows 上工作时,您也需要下载这些本机库,以便使用dotnet publish部署/打包它们。

Here's a little catch though: When you have a full .NET Framework reference in <TargetFramework> or <TargetFrameworks> , then you must specify a single <RuntimeIdentifier> (singular, not plural <RuntimeIdentifiers> ), otherwise you will get an error.不过这里有一个小问题:当您在<TargetFramework><TargetFrameworks>有完整的 .NET Framework 引用时,您必须指定一个<RuntimeIdentifier> (单数,而不是复数<RuntimeIdentifiers> ),否则您将收到错误消息。

For example:例如:

<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>

RID is short for Runtime IDentifier. RID 是运行时标识符的缩写。 RIDs are used to identify target operating systems where an application or asset (that is, assembly) will run. RID 用于标识应用程序或资产(即程序集)将在其中运行的目标操作系统。 They look like this: "ubuntu.14.04-x64", "win7-x64", "osx.10.11-x64".它们看起来像这样:“ubuntu.14.04-x64”、“win7-x64”、“osx.10.11-x64”。 For the packages with native dependencies, it will designate on which platforms the package can be restored.对于具有本机依赖项的包,它将指定可以在哪些平台上恢复包。

More in docs 文档中的更多内容

First change to proper RID from win7 to win7-x64 or win7-x86 .首先将正确的 RID 从win7更改为win7-x64win7-x86 Next add other RID like ubuntu.接下来添加其他 RID,如 ubuntu。 For example:例如:

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

Target framework looking good.目标框架看起来不错。 For more readdocs更多阅读文档

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM