.NET Core RuntimeIdentifier与TargetFramework [英] .NET Core RuntimeIdentifier vs TargetFramework

查看:373
本文介绍了.NET Core RuntimeIdentifier与TargetFramework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我刚刚从VS2015迁移,现在无法发布我的Web api,因为看起来我应该只使用一个目标框架。此外,我无法指定多个RID。
所有这些变化的事情使我感到沮丧。从头做起的任何事情,都不应一再克服。



我只想在Windows上开发Web-api,在此处运行xUnit测试,然后将Web-api部署在linux(ubuntu)服务器上运行。
我应该在csproj的两个参数中输入什么?
带有良好解释的链接倍受赞赏。



Update1



我有引用了.net核心库的Web api。从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):
错误:如果不指定$ b $,则不支持发布目标b目标框架。当前项目针对多个框架,
请指定已发布应用程序的框架。


但是我在作为 netcoreapp1.1 发布。
好​​。然后,我用< PropertyGroup Condition = $(TargetFramework)’==’netcoreapp1.1’>更新了csproj
< RuntimeIdentifier> ubuntu.16.10-x64< / RuntimeIdentifier>
< / PropertyGroup>
,如下所示。
但是现在我什至无法构建应用,出现错误:


5> C:\Program Files(x86) \Microsoft Visual
Studio\2017\企业\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



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

如何发布我的网络应用?

解决方案

< TargetFramework> (或< TargetFrameworks> 您想要多个目标,例如 net451 ,一个或多个 netstandard1.x 等)。对于每个< TargetFramework> / < TargetFrameworks> 条目,将创建一组程序集并将其放置在<$ c内$ c> bin\Debug\< targetframeworkid> )。



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



它用于构建和NuGet恢复。也就是说,您不能在.NET Core项目中使用仅 net451 库(例如, netstandard 1.1 -可以在 net451 项目中使用 netstandard1.1 )。



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



< RuntimeIdentifiers> win7-x64; win7-x86; ubuntu.16.10-x64< / RuntimeIdentifiers> 将为Win7(x64和x86)版本和x64制作nuget恢复包。仅适用于ubuntu。这是必需的,因为在Windows上工作时,您也需要下载这些本机库,以便使用 dotnet publish 进行部署/打包。



这里有一个小问题:当您在< TargetFramework> < TargetFrameworks> ,则必须指定单个< RuntimeIdentifier> (单数而不是复数< RuntimeIdentifiers> ),否则会出现错误。



例如:

 < PropertyGroup> 
< TargetFrameworks> netstandard1.0; net451< / TargetFrameworks>
< RuntimeIdentifiers> win7-x64; win7-x86; ubuntu.16.10-x64< / RuntimeIdentifiers>
< / PropertyGroup>

< ;!-此项仅用于.NET Framework 4.5.1输出->
< PropertyGroup Condition ='$(TargetFramework)'=='net451'>
< RuntimeIdentifier> win7-x64< / RuntimeIdentifier>
< / PropertyGroup>


Can someone explain the purpose of this two in csproj file (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. In addition I can't specify multiple RIDs. 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. What I should put in both parameters in csproj ? Links with good explanation is highly appreciated.

Update1

I have web api with referenced .net core libraries. Everything where migrated from VS2015. Now in root project I have <TargetFrameworks>netcoreapp1.1;net461</TargetFrameworks>. When I publish via VS2017 I got error:

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. 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. OK. Then I updated my csproj with <PropertyGroup Condition="$(TargetFramework)'=='netcoreapp1.1'"> <RuntimeIdentifier>ubuntu.16.10-x64</RuntimeIdentifier> </PropertyGroup> as suggested below. 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'. Ensure you have restored this project for TargetFramework='netcoreapp1.1' and RuntimeIdentifier='ubuntu.16.10-x64'.

I just want develop with VS2017 at windows 8.1/windows7 and deploy to ubuntu 16.10. What I'm doing wrong ?

Update2

I have 8 projects in solution. 3 of them are xUnit tests. Thus we have 5 projects. 4 of these 5 are class libraries and 1 is my web-app. All 4 class libraries have this:

<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). Per <TargetFramework> / <TargetFrameworks> entry one set of assemblies will be created and located inside 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 i.e. .NET Core because it's unsupported.

It is used for both, building and NuGet restore. i.e. 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).

<RuntimeIdentifier> / <RuntimeIdentifiers> on the other side is used for NuGet mainly. It tells NuGet which packages you need. 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). This includes non-managed dlls and *.so (Linux) files.

i.e. <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. 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.

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.

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>

这篇关于.NET Core RuntimeIdentifier与TargetFramework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆