publich self contained application console application in dotnet core

1.什么是self contained.
简单来说,就是即便在设备上没装.net core runtime环境,也能跑.net core程序。不像现在的.net framework程序,必须要装对应版本的.net framework才能运行程序。而且,支持跨平台发布self contained程序。
实际上,self contained程序在发布时,dotnet core runtime以及与程序运行有关且与系统平台无关的所有依赖项都会被一同发布到发布文件夹中,因而设备便可不装.net core runtime运行时环境。
2.如何发布self contained程序
1>.coding 完成后,编辑项目的.csproj文件,设置需要发布的平台

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <RuntimeIdentifiers>win10-x64;osx.10.11-x64;ubuntu.14.04-x64</RuntimeIdentifiers>
  </PropertyGroup>

</Project>

设置内容即为

<RuntimeIdentifiers>win10-x64;osx.10.11-x64;ubuntu.14.04-x64</RuntimeIdentifiers>

其余都是自带的。注意当前.net core不支持32位Linux。
2>发布程序(此处位发布到linux上),采用以下指令

dotnet publish -c release -r ubuntu.14.04-x64

3>项目部署
上述指令会发布项目到

bin\Release\netcoreapp2.1\ubuntu.14.04-x64\publish

文件夹下,部署的时候也应该拷贝此目录到linux上
如果拷贝bin\Release\netcoreapp2.1\ubuntu.14.04-x64,在运行时会报类似下面的错:

Error:
  An assembly specified in the application dependencies manifest (nfz_core.deps.json) was not found:
    package: 'runtime.ubuntu.14.04-x64Microsoft.NETCore.App', version: '2.0.0'
    path: 'runtimes/ubuntu.14.04-x64/lib/netcoreapp2.1/Microsoft.CSharp.dll'

这是由于只有publish文件夹才是发布文件夹,而bin\Release\netcoreapp2.1\ubuntu.14.04-x64文件夹是编译文件夹。
故在部署时,应始终拷贝publish文件夹。

4>程序运行
切换到publish文件夹下,然后执行下面的命令即可运行程序

./项目名称

备注:
程序运行要具备相应的权限。假设要获取权限的文件名为

somefile

linux 获取权限最高权限的指令为

chmod 777 somefile

mac 上为获取权限和执行命令为

获取权限:sudo chmod +x somefile
执行:open somefile

期间报错:
除了上面已经阐述的错误之外,刚重装的linux上运行dotnet core self contained程序时,还遇到了以下错误:

Couldn't find a valid ICU package

通过下面的操作解决:

apt-get install icu-devtools
apt-get install libunwind8 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值