.NET Core RuntimeIdentifier vs TargetFramework

前端 未结 2 1073
独厮守ぢ
独厮守ぢ 2021-02-01 03:57

Can someone explain the purpose of this two in csproj file (VS2017):

netstandard1.6
win7&         


        
相关标签:
2条回答
  • 2021-02-01 04:41

    RID is short for Runtime IDentifier. RIDs are used to identify target operating systems where an application or asset (that is, assembly) will run. They look like this: "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. Next add other RID like ubuntu. For example:

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

    Target framework looking good. For more read docs

    0 讨论(0)
  • 2021-02-01 04:54

    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>
    
    0 讨论(0)
提交回复
热议问题