zoukankan      html  css  js  c++  java
  • .NET Core New csproj 如何发布可执行文件

    一、前言

      .NET工具链在最新的Preview3版本中,引入了新的MSBuild项目系统,项目文件又回归了.csproj的XML文件来管理,项目文件、包引用、程序集引用、.NET Core工具集、发布内容定义等内容。本文主要将主要讨论,如何在新的项目系统中(.csproj)发布可执行文件。我们都知道在之前的版本中,项目文件是通过project.json文件来管理项目和包引用的,那么通过删除 dependencies->Microsoft.NETCore.App-> "type": "platform" 子节点,并定义runtimes节点,来发布可执行文件(想了解的朋友可以阅读这篇文章) 。

      所为可执行文件就是在目标机器上,不需要安装.NET Core SDK或任何Runtime,就可以执行的文件。比如在Windows上可以生成 coreapp.exe的可执行文件,而在Linux中可以使用 ./coreapp 来执行。

      原理上这种可执行文件,就是通过一个C++应用程序为载体(宿主),加载CoreCLR,通过CoreCLR再加载任意的程序集,对这里有兴趣的朋友也可以到Github上去看一下CoreCLR中ClrHost的部分。

    二、生成可执行

      在新的.csproj项目文件中,我们要想发布一个可执行文件,就在手动创建名为<RuntimeIdentifiers>的节点,在这个节点下面,添加RuntimeIdentifiers也就是以前的RID定义,RID是描述系统平台的统一命名标示。例如我想要发布的可执行文件的目标系统平台为Win10Mac os 10.11.* 定义如下:

    1. <PropertyGroup>
    2. <RuntimeIdentifiers>win10-x64;osx.10.11-x64</RuntimeIdentifiers>
    3. </PropertyGroup>

      通过如下命令发布各平台的目标可执行文件:

    1. dotnet build -r win10-x64
    2. dotnet build -r osx.10.11-x64

      上面的命令可以生成带有符号文件和调试信息的DEBUG版本,你的应用程序将生成在.inDebug etcoreapp1.0< runtime_identifier>目录下,如果想生成生产环境的最终版本请通过如下命令获取:

    1. dotnet publish -c release -r win10-x64
    2. dotnet publish -c release -r osx.10.11-x64

      通过上述命令生成的Release版本目标执行文件将生成在.in elease etcoreapp1.0<runtime_identifier>目录下,并且每一个目标平台目录下都有生成的可执行文件、发布项目的程序集、.NET Core依赖或必要的文件来保证生成程序的独立可执行。

      我们来看一个新的csproj文件的完整定义:

    1. <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    2. <Import Project="$(MSBuildExtensionsPath)$(MSBuildToolsVersion)Microsoft.Common.props" />
    3. <PropertyGroup>
    4. <OutputType>Exe</OutputType>
    5. <TargetFramework>netcoreapp1.0</TargetFramework>
    6. <VersionPrefix>1.0.0</VersionPrefix>
    7. <DebugType>Portable</DebugType>
    8. <RuntimeIdentifiers>win10-x64;osx.10.11-x64</RuntimeIdentifiers>
    9. </PropertyGroup>
    10. <ItemGroup>
    11. <Compile Include="***.cs" />
    12. <EmbeddedResource Include="***.resx" />
    13. </ItemGroup>
    14. <ItemGroup>
    15. <PackageReference Include="Microsoft.NETCore.App">
    16. <Version>1.0.1</Version>
    17. </PackageReference>
    18. <PackageReference Include="Newtonsoft.Json">
    19. <Version>9.0.1</Version>
    20. </PackageReference>
    21. <PackageReference Include="Microsoft.NET.Sdk">
    22. <Version>1.0.0-alpha-20161102-2</Version>
    23. <PrivateAssets>All</PrivateAssets>
    24. </PackageReference>
    25. </ItemGroup>
    26.  
    27. <Import Project="$(MSBuildToolsPath)Microsoft.CSharp.targets" />
    28. </Project>

    三、RID

      RID是Runtime Identifier的缩写,它用于定义目标操作系统标示。RID会不断的更新,我们可以在CoreFx项目中找到RID定义,常用的RID有如下:

      Windows RIDs

    • Windows 7 / Windows Server 2008 R2
      • win7-x64
      • win7-x86
    • Windows 8 / Windows Server 2012
      • win8-x64
      • win8-x86
      • win8-arm
    • Windows 8.1 / Windows Server 2012 R2
      • win81-x64
      • win81-x86
      • win81-arm
    • Windows 10 / Windows Server 2016
      • win10-x64
      • win10-x86
      • win10-arm
      • win10-arm64

      Linux RIDs

    • Red Hat Enterprise Linux
      • rhel.7.0-x64
      • rhel.7.1-x64
      • rhel.7.2-x64
    • Ubuntu
      • ubuntu.14.04-x64
      • ubuntu.14.10-x64
      • ubuntu.15.04-x64
      • ubuntu.15.10-x64
      • ubuntu.16.04-x64
      • ubuntu.16.10-x64
    • CentOS
      • centos.7-x64
    • Debian
      • debian.8-x64
    • Fedora
      • fedora.23-x64
      • fedora.24-x64
    • OpenSUSE
      • opensuse.13.2-x64
      • opensuse.42.1-x64
    • Oracle Linux
      • ol.7-x64
      • ol.7.0-x64
      • ol.7.1-x64
      • ol.7.2-x64
    • Currently supported Ubuntu derivatives
      • linuxmint.17-x64
      • linuxmint.17.1-x64
      • linuxmint.17.2-x64
      • linuxmint.17.3-x64
      • linuxmint.18-x64

      OS X RIDs

    • osx.10.10-x64
    • osx.10.11-x64
    • osx.10.12-x64

    四、系统依赖

      发布出来的目标平台可执行文件,也是需要依赖系统特性的,接下来我们来看下系统的需要组件有哪些:

    Windows Ubuntu CentOS

    OS X

     
    • Visual C++ Redistributable
    • for Visual Studio 2015
     
    • libunwind8
    • libunwind8-dev
    • gettext
    • libicu-dev
    • liblttng-ust-dev
    • libcurl4-openssl-dev
    • libssl-dev
    • uuid-dev
    • unzip
     
    • deltarpm
    • epel-release
    • unzip
    • libunwind
    • gettext
    • libcurl-devel
    • openssl-devel
    • zlib
    • libicu-devel
    • libssl version 1.0.1



  • 相关阅读:
    linux系统用户登陆时脚本执行顺序
    stm32 win7 64位虚拟串口驱动安装失败解决办法
    python全栈day6
    python全栈day5
    江湖救急(处理域名未备案网站问题)
    python全栈day4
    python全栈day3
    python全栈day2
    python全栈day1
    PHP 跨域之header
  • 原文地址:https://www.cnblogs.com/xieqing/p/6520092.html
Copyright © 2011-2022 走看看