.Net Core cross-platform: a simple multi-platform program (windows, Linux, osx) released

.Net Core cross-platform: a simple multi-platform program (windows, Linux, osx) released

 

 

.Net Core 3.0 was September 23, 2019 release, it includes some new features, see in particular Announcing .NET Core 3.0

.NET Core is a cross-platform, high-performance, open source framework for building modern, cloud-based application framework for the Internet connection, which has about the advantages and features:

 

Cross-Platform:. NET Framework application runs only on the Windows platform, and .NET Core and applications can be developed on the Windows platform across Windows, Linux and other macOS or different platforms. ASP.NET 4.x applications can only be hosted on IIS, and ASP.NET Core applications can be hosted in IIS, Apache, Docker or your own process of self-hosted in. From a development point of view, you can build .NET Core applications using Visual Studio or Visual Studio Code. Developers can use a third-party editor, such as Sublime.

 

MVC and Web API is a unified programming model : by ASP.NET Core, we use the same unified programming model to create a MVC-style Web applications and ASP.NET Web API. In both cases, Controller we create will inherit from the same base class Controller and return IActionResult. As the name suggests, IActionResult is an interface, it has a variety of implementations. ViewResult and JsonResult exemplary only achieve two results IActionResult built type interface. Therefore, for Web API, the controller returns JsonResult, for MVC-style Web applications, the controller returns ViewResult. Their essence is the same, the controller may be considered to be returned ViewResult Content-Type is different. Some namespaces MVC and Web API also merged.

Built dependency injection: .NET Core built dependency injection. .NET Core cross-platform is just one of a purpose, which is to rewrite the frame, you will find a lot of follow-up component, use third-party libraries is through dependency injection. The official tutorial examples are also basic dependency injection throughout the text.

Easy to test: Create a Web application and a unified programming model for Web API's built-in dependency injection and for easy unit testing ASP.NET Core applications.

Open Source: .NET Core is completely open source, and is actively developed by a team of open source .NET developer community to cooperate with the majority. Therefore, as the majority of the core ASP.NET community behind it is proposed to improve the methods and helps fix errors and problems, which continue to develop. This means that we have a more secure, higher quality software. Github Address: https://github.com/dotnet/core

Modular HTTP request pipeline: the ASP.NET Core provides a modular assembly through the middleware. In ASP.NET Core, we use a middleware component composition to the request and response pipeline. It includes a rich set of built-in middleware components. We can also write our own custom middleware components. 

 

Let's release with a simple Hello World application to windows, Linux, osx three platforms as an example.

Development environment can select windows or osx, in order to demonstrate osx is not installed .net core environment to run .net core applications, osx I will not install visual studio.

Windows environment using Visual Studio 2019, Download https://visualstudio.microsoft.com/zh-hans/

OSX environment using Visual Studio for Mac, Download: https://visualstudio.microsoft.com/zh-hans/vs/mac/

 

New Console Application (.NET Core), as shown below:

 

 

 

 

Project templates automatically generate a Hello World console program, to demonstrate multi-platform, with some slight modifications to the code:

 

 

 

On the Project menu select the right solutions Published:

 

 

 

 

 

 

 

 

Configuration instructions:

Deployment mode is divided into separate deployment and framework dependent, independent deployment may run .netcore not installed on the target platform environment; framework relies .netcore need to install the operating environment on the target platform, but also to consider the version of the problem.

That release target runtime platform, win-x86, win-x64, win-arm, Linux -86, Linux-x64, osx-x64, etc.

Here are three platforms to be published, three goals were established platform corresponding configuration:

 

 

 

 

 

 

 

Edit HelloConsoleApp.csproj project file:

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

 

  <PropertyGroup>

    <OutputType>Exe</OutputType>

    <TargetFramework>netcoreapp3.0</TargetFramework>

    <PublishTrimmed>true</PublishTrimmed>

    <PublishReadyToRun>false</PublishReadyToRun>

    <PublishSingleFile>true</PublishSingleFile>

    <RuntimeIdentifier>win-x64</RuntimeIdentifier>

  </PropertyGroup>

 

</Project>

 

选中对应的配置文件,依次发布,发布目录如下

 

 

 

 

先看Windows的

 

 

 

可以看到,发布后一个单独的可执行文件,25.8M !

遥想N年前,很多人就想:

  1. 如何把.net程序打包到没有安装.net运行环境的Windows运行?
  2. 如何把依赖的一堆dll打包成一个可执行文件?

现在,这些问题统统已经解决,不但如此,还能同一份代码发布到Linux、OSX等其它OS运行!

 

上图,Window下:

 

 

 

 

Linux 发布目录也是一个可以独立运行的可执行程序:

 

 

 

 

将其放到Linux系统运行:

 

 

 

 

 

输出系统版本为Unix 4.15.0.29

 

 

Osx 环境下:

 

 

 

 

运行:

 

 

 

 

 

 

Osx 上面并没有安装.netcore

 

 

 

 

目标平台无需安装.netcore运行环境。

至此,一个简单的.netcore跨平台程序发布示例完成。

 

Guess you like

Origin www.cnblogs.com/sndnnlfhvk/p/11613685.html