. NET Core Multi-platform development experience [2]: Mac OS X

Source: Internet
Author: User
Tags dotnet hosting

In addition to Microsoft's own Windows platform,. NET Core provides good support for Mac OS and a variety of Linux (RHEL, Ubuntu, Debian, Fedora, CentOS, SUSE, etc.) Let's take a look at using a Mac to develop a. NET core application, before we routinely build our development environment.

First, install the development environment

Like Windows, if we were to develop a. NET core application on a Mac in the afternoon, we just had to install the. NET Core SDK and the appropriate IDE. For the former, we can directly from the Microsoft Official site (Https://www.microsoft.com/net/core#macos), after the SDK installation we will have. NET core for Mac OS Runtime and corresponding tools, which are included in the. Net Core multiplatform development Experience [1]: Windows frequently used command-line tools dotnet. After the. NET Core SDK Installation is complete, we can execute the dotnet–info command to verify that the SDK was successfully installed. If the SDK is installed successfully, we can see that the runtime environment information shown is matched to the currently installed operating system, as shown in the basic information.

For developers of. NET core Apps for Mac OS X, they also have a wide range of choices on the IDE. First, Visual Studio now has a Mac version, and although there is a gap between functionality and stability with the version of Windows, it is a boon for Mac users accustomed to Visual Studio. In addition, we can choose the free visual Studio Code, if you do not like this style of the IDE, you can also choose JetBrains Rider.

Ii. creating a. NET Core program using the command line

For Windows and Mac users, the development experience for them is basically consistent because the. NET Core SDK provides the command line (primarily the dotnet command-line tool) with a consistent API definition, and they can choose the same IDE (Visual Studio and Visual Studio Code). Let's start with the dotnet New Scaffold command-line tool to create a console app named HelloWorld as shown.

(right) is shown in the project structure of the console app created by the scaffolding command line, and you can see that it has the same file structure as the app we created on Windows to perform the same command. Not only that, the resulting file content is also exactly the same, and the contents of the Helloworld.csproj file and program file Program.cs file that defines the project are as follows.

Helloworld.csproj

<ProjectSDK= "MICROSOFT.NET.SDK">  <PropertyGroup>    <OutputType>Exe</OutputType>    <targetframework>netcoreapp2.0</targetframework>  </PropertyGroup></Project>

Pprogram.cs

using System;     namespace helloworld{    class  program    {        staticvoid Main (  String[] args)        {            Console.WriteLine ("Hello world! " );        }    }}

We can start the console program directly by executing the dotnet command line without having to make any changes to the created application. As shown in the following code snippet, after switching the current directory to the root of the project where the console app is located, we directly execute the dotnet Run command to start the program, and then the "Hello World" string that is output to the console in the main method is printed.

Iii. transforming into ASP. NET Core MVC application

In the. NET core multi-platform development experience [1]: Windows, we demonstrated how to transform a console application created from a scaffold command line into an ASP. NET core MVC application, since. So the same way that we've modified the ASP. NET Core MVC app can also run on Mac OS, and we'll verify it later.

We first need to install the required NuGet packages, although the two core types of webhost and webhostbuilder are defined in the NuGet package "Microsoft.AspNetCore.Hosting", but the ASP. The MVC framework provides the implementation of the "MICROSOFT.ASPNETCORE.MVC" dependency on this NuGet package, so we just need to install the latter. In addition, we need to install the NuGet package "Microsoft.AspNetCore.Server.Kestrel" that provides cross-platform server kestrelhttpserver. The installation of several NuGet packages described above also applies here, and we now choose how to install the command line, for which we will execute the dotnet Add Package command as shown to install the two NuGet packages on the current project.

Regardless of the way we use it, the NuGet package installed will eventually be reflected in the. cspoj file that defines the project, so open the Helloworld.csproj file and we'll see the following, the two NuGet packages we installed are embodied in <ItemGroup> Two < packagereference> elements under a node.

<ProjectSDK= "MICROSOFT.NET.SDK">  <PropertyGroup>    <OutputType>Exe</OutputType>    <targetframework>netcoreapp2.0</targetframework>  </PropertyGroup>  <ItemGroup>    <packagereferenceInclude= "MICROSOFT.ASPNETCORE.MVC"Version= "2.0.0" />    <packagereferenceInclude= "Microsoft.AspNetCore.Server.Kestrel"Version= "2.0.0" />  </ItemGroup></Project>

After installation of the required NuGet package, we chose the appropriate IDE or plain text editor to make the following modifications to the Program.cs, then our application became a simple ASP. NET Core MVC application. After the above introduction, I believe that the reader friends have been very clear about the meaning of each line of code, so here we do not explain the introduction.

usingMicrosoft.AspNetCore.Builder;usingMicrosoft.AspNetCore.Hosting;usingMICROSOFT.ASPNETCORE.MVC;usingMicrosoft.Extensions.DependencyInjection;namespacehelloworld{classProgram {Static voidMain (string[] args) {            NewWebhostbuilder (). Usekestrel (). Configureservices (Svcs=Svcs. ADDMVC ()). Configure (App=app. USEMVC ()). Build ().        Run (); }    }     Public classHellocontroller {[HttpGet ("/hello/{name}")]         Public stringSayHello (stringname) {            return$"Hello, {name}"; }    }}

So far, all the programming has been done, and we just need to execute the dotnet Run command in a familiar way to start the program. After the program is started, the address "http://localhost:5000/hello/foobar" is accessed using the browser, and we get the output as shown in 1-21.

. NET Core Multi-platform development experience

. NET Core Multi-platform development experience [1]: Windows
. NET Core Multi-platform development experience [2]: Mac OS X
. NET Core Multi-platform development experience [3]: Linux
. NET Core Multi-platform development experience [4]: Docker

. NET Core Multi-platform development experience [2]: Mac OS X

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.