.net跨平台运行实践
一个偶然的机会,一个朋友想做一个程序,同时支持windows和linux,本来想用go来写,奈何不太熟练,突然想到.net不是也支持跨平台了吗,还没有操作过,刚好可以试验一下。
最新的.net 6已经跨平台了,从.net core 3.0后,不再叫.net core 4.0了,而是统一为.net 5,.net 6等。
演示程序如下:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!输入Q退出。");
while (true)
{
string str = Console.ReadLine();
if (str.ToLower() == "q")
{
break;
}
else
{
Console.WriteLine("输入Q退出。");
}
}
}
}
}
以下为生成文件

在windows上运行就不用说了,直接编译,点击exe就可以了。
那么在linux上怎么运行呢,这里以debian为例演示
先安装运行时环境
官方安装教程
https://docs.microsoft.com/en-us/dotnet/core/install/linux-debian
根据操作系统版本,选择相应的教程,我这里操作系统是用和debian 10

上面是安装SDK和安装运行时的,一般开发人员可以安装SDK,用于开发使用。
普通用户,仅仅是使用的话,就用运行时就可以了。SDK是包含运行时的。
sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y aspnetcore-runtime-6.0
下面有一个提示,大概意思就是说你也可以安装.net运行时,.net运行时不包含asp.net core(从这句话理解,好像是说dotnet-runtime比aspnetcore-runtime要小),安装方式就是
sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y dotnet-runtime-6.0
安装过程,注意我实际安装的是.net5.0,因为我用VS2019默认是.net5.0
root@debiantest1:/etc/apt/sources.list.d# sudo apt-get update; sudo apt-get install -y apt-transport-https && sudo apt-get update && sudo apt-get install -y dotnet-runtime-5.0
Hit:1 http://mirrors.ustc.edu.cn/debian stretch-updates InRelease
Hit:2 http://mirrors.ustc.edu.cn/debian stretch-backports InRelease
Ign:3 http://mirrors.ustc.edu.cn/debian stretch InRelease
Hit:4 http://mirrors.ustc.edu.cn/debian-security stretch/updates InRelease
Hit:5 http://mirrors.ustc.edu.cn/debian stretch Release
Hit:6 http://security.debian.org/debian-security buster/updates InRelease
Hit:8 http://mirrors.huaweicloud.com/debian buster InRelease
Hit:9 http://mirrors.huaweicloud.com/debian buster-updates InRelease
Hit:10 https://download.docker.com/linux/debian buster InRelease
Hit:11 https://packages.microsoft.com/debian/10/prod buster InRelease
Hit:12 http://deb.nodesource.com/node_13.x buster InRelease
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
apt-transport-https is already the newest version (1.8.2.3).
0 upgraded, 0 newly installed, 0 to remove and 141 not upgraded.
Hit:1 http://mirrors.ustc.edu.cn/debian stretch-updates InRelease
Hit:2 http://mirrors.ustc.edu.cn/debian stretch-backports InRelease
Ign:3 http://mirrors.ustc.edu.cn/debian stretch InRelease
Hit:4 http://mirrors.ustc.edu.cn/debian-security stretch/updates InRelease
Hit:5 http://mirrors.ustc.edu.cn/debian stretch Release
Hit:7 http://mirrors.huaweicloud.com/debian buster InRelease
Hit:8 http://mirrors.huaweicloud.com/debian buster-updates InRelease
Hit:9 https://download.docker.com/linux/debian buster InRelease
Hit:10 https://packages.microsoft.com/debian/10/prod buster InRelease
Hit:11 http://deb.nodesource.com/node_13.x buster InRelease
Hit:12 http://security.debian.org/debian-security buster/updates InRelease
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
dotnet-host dotnet-hostfxr-5.0 dotnet-runtime-deps-5.0
The following NEW packages will be installed:
dotnet-host dotnet-hostfxr-5.0 dotnet-runtime-5.0 dotnet-runtime-deps-5.0
0 upgraded, 4 newly installed, 0 to remove and 141 not upgraded.
Need to get 22.2 MB of archives.
After this operation, 70.8 MB of additional disk space will be used.
Get:1 https://packages.microsoft.com/debian/10/prod buster/main amd64 dotnet-host amd64 6.0.3-1 [55.7 kB]
Get:2 https://packages.microsoft.com/debian/10/prod buster/main amd64 dotnet-hostfxr-5.0 amd64 5.0.15-1 [140 kB]
Get:3 https://packages.microsoft.com/debian/10/prod buster/main amd64 dotnet-runtime-deps-5.0 amd64 5.0.15-1 [2,648 B]
Get:4 https://packages.microsoft.com/debian/10/prod buster/main amd64 dotnet-runtime-5.0 amd64 5.0.15-1 [22.0 MB]
Fetched 22.2 MB in 12s (1,849 kB/s)
Selecting previously unselected package dotnet-host.
(Reading database ... 59387 files and directories currently installed.)
Preparing to unpack .../dotnet-host_6.0.3-1_amd64.deb ...
Unpacking dotnet-host (6.0.3-1) ...
Selecting previously unselected package dotnet-hostfxr-5.0.
Preparing to unpack .../dotnet-hostfxr-5.0_5.0.15-1_amd64.deb ...
Unpacking dotnet-hostfxr-5.0 (5.0.15-1) ...
Selecting previously unselected package dotnet-runtime-deps-5.0.
Preparing to unpack .../dotnet-runtime-deps-5.0_5.0.15-1_amd64.deb ...
Unpacking dotnet-runtime-deps-5.0 (5.0.15-1) ...
Selecting previously unselected package dotnet-runtime-5.0.
Preparing to unpack .../dotnet-runtime-5.0_5.0.15-1_amd64.deb ...
Unpacking dotnet-runtime-5.0 (5.0.15-1) ...
Setting up dotnet-host (6.0.3-1) ...
Setting up dotnet-runtime-deps-5.0 (5.0.15-1) ...
Setting up dotnet-hostfxr-5.0 (5.0.15-1) ...
Setting up dotnet-runtime-5.0 (5.0.15-1) ...
Processing triggers for man-db (2.8.5-2) ...
安装后验证
root@debiantest1:/etc/apt/sources.list.d# dotnet --info
Host (useful for support):
Version: 5.0.15
Commit: 3065735be7
.NET SDKs installed:
No SDKs were found.
.NET runtimes installed:
Microsoft.NETCore.App 5.0.15 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
To install additional .NET runtimes or SDKs:
https://aka.ms/dotnet-download
运行程序
root@debiantest1:/data/demo1# dotnet ConsoleApp1.dll
Hello World!输入Q退出。
输入Q退出。
输入Q退出。
输入Q退出。
输入Q退出。
输入Q退出。
q
可以看到已经运行成功了。和windows的效果一样。
.net跨平台运行实践的更多相关文章
- ASP.NET跨平台最佳实践
前言 八年的坚持敌不过领导的固执,最终还是不得不阔别已经成为我第二语言的C#,转战Java阵营.有过短暂的失落和迷茫,但技术转型真的没有想象中那么难.回头审视,其实单从语言本身来看,C#确实比Java ...
- 开源一个跨平台运行的服务插件 - TaskCore.MainForm
本次将要很大家分享的是一个跨平台运行的服务插件 - TaskCore.MainForm,此框架是使用.netcore来写的,现在netcore已经支持很多系统平台运行了,所以将以前的Task.Main ...
- cross-env 兼容跨平台运行脚本
https://www.npmjs.com/package/cross-env 跨平台运行脚本 The problem Most Windows command prompts will choke ...
- 物联网框架ServerSuperIO在.NetCore实现跨平台的实践路线
正所谓天下大势,不跟风不行.你不跨平台,很low嘛.java说:你们能跨嘛,跨给我看看.C#说:不要强人所难嘛.java说:能部署在云上吗?docker?微服务?C#说:不要强人所难嘛.java说:你 ...
- Flutter 实现原理及在马蜂窝的跨平台开发实践
一直以来,跨平台开发都是困扰移动客户端开发的难题. 在马蜂窝旅游 App 很多业务场景里,我们尝试过一些主流的跨平台开发解决方案, 比如 WebView 和 React Native,来提升开发效率和 ...
- 跨平台技术实践案例: 用 reactxp 重写墨刀的移动端
Authors: Gao Cong, Perry Poon Illustrators: Shena Bian April 20, 2019 重新编写,又一次,我们又一次重新编写了移动端应用和移动端 ...
- 跨平台运行 Rafy 首次部署记录
一直想在 Linux 上使用 MONO 试试运行 Rafy,最近因为业务需要,总算是真正地试验了一次.下面是本次部署记录的一些要点. Linux 这次部署,我是和两位同事一起来试验的.由于我们对 Li ...
- 跨平台运行ASP.NET Core 1.0
前言 首先提一下微软更名后的叫法: ASP.NET 5 更名为 ASP.NET Core 1.0 .NET Core 更名为 .NET Core 1.0 Entity Framework 7 更名为 ...
- Java为什么能跨平台运行
因为它有虚拟机(JVM),JAVA程序不是直接在电脑上运行的,是在虚拟机上进行的,每个系统平台都是有自己的虚拟机(JVM),所以JAVA语言能跨平台. 1, java代码不是直接运行在CPU上,而是运 ...
- 跨平台运行ASP.NET Core 1.0(转载)
前言 首先提一下微软更名后的叫法: ASP.NET 5 更名为 ASP.NET Core 1.0 .NET Core 更名为 .NET Core 1.0 Entity Framework 7 更名为 ...
随机推荐
- 从零开始学Spring Boot系列-前言
从零开始学Spring Boot系列-前言 在数字化和信息化的时代,Java作为一种成熟.稳定且广泛应用的编程语言,已经成为构建企业级应用的首选.而在Java生态系统中,Spring框架无疑是其中最为 ...
- MySQL写入SQL整个执行流程
innodb存储引擎中一条sql写入的详细流程 第0步:会先去看缓冲区有没有这条数据,如果有就不进行缓存,直接进入第三步. 第1步:会将要修改的那一行数据所在的一整页加载到缓冲池Buffe ...
- Go微服务框架go-kratos实战学习08:负载均衡基本使用
微服务框架 go-kratos 中负载均衡使用 一.介绍 在前面这篇文章 负载均衡和它的算法介绍,讲了什么是负载均衡以及作用.算法介绍. go-kratos 的负载均衡主要接口是 Selector,它 ...
- 【系统选型】OA需求分析,OA系统选型及各供应商对比。
去年公司内部做OA信息化升级,需要更新换代一下OA系统,当时OA选型整理下来的资料分享一下. 需求调研整理后如下: 一共四个模块需要更新&升级 : OA模块(包括行政) + 合同模块 + 费 ...
- 【Azure 服务总线】如何批量删除Azure Service Bus中的Topics(数量较多,需要过滤后批量删除)
问题描述 Azure Service Bus 的门户操作页面上,是否可以批量删除其中的Topics呢? 问题解答 Azure Service Bus门户或Service Bus Explorer工具没 ...
- 【Azure 环境】如何解决Principal 2330xxxxxxxxxxxxxxxxxxxx31efc5 does not exist in the directory xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx问题
问题描述 在使用 Key Vault 和 Azure CLI 管理存储帐户密钥的官方文档中,其中有一步是"向 Key Vault 授予对你的存储帐户的访问权限", 其中CLI命令中 ...
- 微软应用商店错误代码“0x80131500”怎么修复?
处理方法一 打开"运行"输入 inetcpl.cpl ("WINDOWS"+"R"键,输入 inetcpl.cpl亦可),点开高级往下拉,勾 ...
- 文心一言 VS 讯飞星火 VS chatgpt (209)-- 算法导论15.4 6题
六.设计一个 O(nlgn) 时间的算法,求一个 n 个数的序列的最长单调递增子序列.(提示:注意到,一个长度为 i 的候选子序列的尾元素至少不比一个长度为 i-1 候选子序列的尾元素小.因此,可以在 ...
- stm32f103 实现LCD显示及分析
前记 stm32 f103 在很多物联网系统中,状态显示是一个很重要的部分,在配合其它的系统做物联网设备时候,有一个状态显示,不仅仅是显得高大上,并且能够让你的系统变得更加人性化,这对于做设备的来 ...
- 解决jenkins Git Publisher自动打tag的问题
简单配置一下 然后开始构建,然后报错如下 The recommended git tool is: NONE using credential 647ee613-5032-4894-aaeb-fe07 ...