NET Core 環境搭建和命令列CLI入門[轉]

來源:互聯網
上載者:User

標籤:10.10   schedule   display   depend   label   variant   base   package   initial   

 NET Core 環境搭建和命令列CLI入門時間:2016-07-06 01:48:19      閱讀:258      評論:0      收藏:0      [點我收藏+]  

標籤:

NET Core 環境搭建和命令列CLI入門

2016年6月27日.NET Core & ASP.NET Core 1.0在Redhat峰會上正式發布,社區裡湧現了很多文章,我也計劃寫個系列文章,原因是.NET Core的入門門檻相當高,很有必要寫個深入淺出的系列文章,本節內容協助你入門。我將可能用Windows做開發環境,也可能用Linux/Mac,但是所有的dotnet CLI命令都是跨平台的,我們在windows/Linux/mac平台上開發跨平台的應用。

 

安裝.NET Core

.NET Core 包括.NET Core Runtime 和 .NET Core SDK:

  1. .NET Core = 應用運行依賴的 .NET Core Runtime
  2. .NET Core SDK = 使用.NET Core開發應用.NET Core Runtime 和 SDK+CLI(Software Development Kit/Command Line Interface) 工具

請到dotnet官方網站dot.net (dot dot net),非常的好記,這個網站也是你入門學.NET Core的入口網站,記住這是個必須要去網站。.NET Core 下載的具體地址:https://www.microsoft.com/net/download#core,這裡還列出了注意事項:

  1. 雖然.NET Core Runtime 和基礎庫已經1.0 RTM,但是開發工具鏈(.NET Core CLI,Visual studio 和Visual Studio Code) 還是預覽版,具體可以參看https://blogs.msdn.microsoft.com/dotnet/2016/05/06/net-core-rc2-improvements-schedule-and-roadmap/
  2. 在Windows Servers上部署應用 ,可以單獨安裝ASP.NET Core Module for IIS 而不需要安裝.NET Core runtime,可以通過命令列DotNetCore.1.0.0-WindowsHosting.exe OPT_INSTALL_REDIST=0
  3. Mac 系統的最低要求是macOS 10.11 (El Capitan)
  4. .NET Core 在Red Hat 系列伺服器上支援的要 RHEL 7 Server,包括CentOS 7 以上,具體參看 https://access.redhat.com/documentation/en/dot-net-core/
  5. .NET Core 在Ubuntu上面的支援 需要 Ubuntu 14.04 LTS 和  Ubuntu 16.04 LTS
  6. 如果之前安裝了.NET Core的beta,rc1,rc2的版本,在安裝之前需要把他們卸載掉,具體參見 stack overflow 的文章。

.NET Core 的具體安裝方法可以參看文章 .Net Core 系列:1、環境搭建。

 

dotnet Command Line Interface(CLI)

.NET Core 的dotnet 命令列介面(CLI)非常重要,是我們開發,運營都要使用的一套工具,Microsoft 為我們提供了這個命令列工具以供我們在開發程式中使用,它主要用來進行對代碼的編譯、NuGet 包的管理、程式的運行、測試等等。簡單的說 :當一個新人坐在座位上開始學習node, python, ruby, golang 或者其它任何東西時,對於絕大多數人來說,按照他們的經驗就應該如此。使用.NET應當被看做是一件簡單的不能再簡單的事了。學習並且使用.NET 的夥伴們有著在Go或者Ruby上有相同的經驗。這篇文章 .NET Core dotnet 命令大全 介紹了命令列的使用方法,dotnet run 命令介紹 更詳細的介紹了dotnet run命令。練習例子可以用文章 通過幾個Hello World感受.NET Core全新的開發體驗。

 

我們就來通過一個Hello World例子來學習下dotnet 命令列,我們安裝好.NET Core 1.0,就可以做下面的事情了:

>dotnet new
>dotnet restore
>dotnet run

想象著和我一塊兒,當你把這個和在Mac, Windows, Linux上啟動並執行 Visual Studio代碼相比較時,那麼你已經獲得了一個非常精彩的故事。可以很容易的在很多地方運行開源的.NET 代碼。

下面是一段比較長的的代碼,建立一個控制台應用,只要在頂部輸入“dotnet”,就可以得到很多能用的東西。

[[email protected] helloworld]# dotnet

Microsoft .NET Core Shared Framework Host

  Version  : 1.0.1
  Build    : cee57bf6c981237d80aa1631cfe83cb9ba329f12

Usage: dotnet [common-options] [[options] path-to-application]

Common Options:
  --help                           Display .NET Core Shared Framework Host help.
  --version                        Display .NET Core Shared Framework Host version.

Options:
  --fx-version <version>           Version of the installed Shared Framework to use to run the application.
  --additionalprobingpath <path>   Path containing probing policy and assemblies to probe for.

Path to Application:
  The path to a .NET Core managed application, dll or exe file to execute.

If you are debugging the Shared Framework Host, set ‘COREHOST_TRACE‘ to ‘1‘ in your environment.

To get started on developing applications for .NET Core, install .NET SDK from:
  http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409

[[email protected] ~]# mkdir helloworld
[[email protected] ~]# cd helloworld
[[email protected] helloworld]# dotnet new
Created new C# project in /root/helloworld.
[[email protected] helloworld]# ls
Program.cs  project.json
[[email protected] helloworld]# vi Program.cs
[[email protected] helloworld]# dotnet restore
log  : Restoring packages for /root/helloworld/project.json...
log  : Writing lock file to disk. Path: /root/helloworld/project.lock.json
log  : /root/helloworld/project.json
log  : Restore completed in 2277ms.
[[email protected] helloworld]# dotnet run
Project helloworld (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling helloworld for .NETCoreApp,Version=v1.0

Compilation succeeded.
    0 Warning(s)
    0 Error(s)

Time elapsed 00:00:03.0002808


Hello World!
[[email protected] helloworld]#

通過上面這幾個基本命令就把我們的.NET Core 應用運行起來了。


 

dotnet new

new 命令用於建立.NET項目或者是應用


[[email protected] ~]# dotnet new --help
.NET Initializer

Usage: dotnet new [options]

Options:
  -h|--help             Show help information
  -l|--lang <LANGUAGE>  Language of project [C#|F#]
  -t|--type <TYPE>      Type of project

可以用這個命令建立幾個不同類型的的應用類型,支援C#和F#,C#語言支援的項目類型如下:

  • Console
  • Web
  • Library
  • xUnit Test

 

dotnet restore

restore命令使用NuGet還原在專案檔project.json 中定義的依賴關係和項目特定的工具。


[[email protected] ~]# dotnet restore --help


Usage: nuget3 restore [arguments] [options]

Arguments:
  [root]  List of projects and project folders to restore. Each value can be: a path to a project.json or global.json file, or a folder to recursively search fo                                                          r project.json files.

Options:
  -h|--help                       Show help information
  --force-english-output          Forces the application to run using an invariant, English-based culture.
  -s|--source <source>            Specifies a NuGet package source to use during  the restore.
  --packages <packagesDirectory>  Directory to install packages in.
  --disable-parallel              Disables restoring multiple projects in parallel.
  -f|--fallbacksource <FEED>      A list of packages sources to use as a fallback.
  --configfile <file>             The NuGet configuration file to use.
  --no-cache                      Do not cache packages and http requests.
  --infer-runtimes                Temporary option to allow NuGet to infer RIDs  for legacy repositories
  -v|--verbosity <verbosity>      The verbosity of logging to use. Allowed values: Debug, Verbose, Information, Minimal, Warning, Error.
  --ignore-failed-sources         Only warning failed sources if there are packages meeting version requirement
[[email protected] ~]#

正如你可以看到從"用法:"上面的一行,還原命令只調用到 nuget3 可執行程式,通常您不需要修改這些選項,如果您使用的自訂軟體包源,要麼因為您正在使用預發布版本的 Microsoft 庫或您的組織使用其自己的軟體包源,您可能需要指定使用-s 參數的軟體包源。

運行 dotnet 還原產生一個鎖檔案 (project.json.lock),其中包括有關所有被恢複的軟體包的詳細的資訊。

 

dotnet build

build命令會把項目和他所依賴的項目編譯成一個二進位檔案,預設情況下二進位檔案是Intermediate Language (IL) 和.dll 為副檔名。編譯過程依賴於已經存在鎖檔案(project.json.lock),這是restore命令產生的。

為了產生一個可執行檔應用程式,您需要確保該項目配置的編譯選項設定應用的進入點︰

 

 "buildOptions": {    "emitEntryPoint": true  },
運行命令可以看到使用方法: 
> dotnet build --help.NET BuilderUsage: dotnet build [arguments] [options]Arguments:<PROJECT>  The project to compile, defaults to the current directory. Can be one or multiple paths to project.json, project directory or globbing patter that matches project.json filesOptions:-h|--help                           Show help information-o|--output <OUTPUT_DIR>            Directory in which to place outputs-b|--build-base-path <OUTPUT_DIR>   Directory in which to place temporary outputs-f|--framework <FRAMEWORK>          Compile a specific framework-r|--runtime <RUNTIME_IDENTIFIER>   Produce runtime-specific assets for the specified runtime-c|--configuration <CONFIGURATION>  Configuration under which to build--version-suffix <VERSION_SUFFIX>   Defines what `*` should be replaced with in version field in project.json--build-profile                     Set this flag to print the incremental safety checks that prevent incremental compilation--no-incremental                    Set this flag to turn off incremental build--no-dependencies                   Set this flag to ignore project to project references and only build the root project

你可以使用-f 選項來指定你想要編譯為一個特定架構。這一架構必須在專案檔中定義。-C 選項允許您指定要使用的配置。它將預設為Debug,但您可以指定為Release。

 

dotnet run

大多數情況下,你都是跳過上面的restore,build,直接使用dotnet run命令來運行程式,無論是否修改了應用程式,都是重新編譯產生應用並運行。

> dotnet run --help.NET Run CommandUsage: dotnet run [options] [[--] <arg>...]]Options:-h|--help           Show help information-f|--framework      Compile a specific framework-c|--configuration  Configuration under which to build-p|--project        The path to the project to run (defaults to the current directory). Can be a path to a project.json or a project directory
dotnet run 命令介紹 更詳細的介紹了dotnet run命令

-f, --framework

使用提供的 framework 來運行,這個 framework 就是對應project.json檔案中的 frameworks 節點

-c, --configuration [Debug|Release]

配置使用的環境是 Debug 還是 Release,預設為 Debug 模式。

-p, --project [PATH]

指定要啟動並執行項目,它可以是project.json的檔案路徑,可以是包含project.json的路徑,如果沒有指定,預設是當前路徑。

 

dotnet [assemblyname]

可以用dotnet命令運行已經編譯好的應用,把應用路徑作為參數傳遞給dotnet命令

 

> dotnet .\bin\Debug\netcoreapp1.0\helloworld.dll
Hello World!

 

dotnet pack

pack命令編譯項目並產生NuGet包,該操作會產生兩個NuGet程式包:

  • 一個包括已編譯代碼的組件檔
  • 一個包括偵錯符號和已編譯代碼的組件檔

項目依賴的NuGet項目添加到產生nuspec檔案中,預設情況不打包項目之間的參考關聯性,但可以通過更改項目的相關性類型。

 

dotnet publish

發布命令會編譯應用程式並讀取專案檔,然後將結果集的檔案發布到一個目錄。組建目錄的內容將取決於項目的類型,但可以包括一個跨平台的 IL 應用程式和他依賴項,這就是通常用的Portable部署方式,應用程式共用.NET Core運行時環境與程式集依賴,部署的目標機器上需要事先安裝.NET Core SDK,然後用dotnet命令運行程式。或者是每個本機平台的子檔案夾或自包含的應用程式,其中包括目標平台的運行時,這就是Self-contained部署方式。Self-contained部署方式就是每個應用程式內建.NET Core運行時環境與程式集依賴,部署的目標機器不需要安裝.NET Core SDK,將應用程式檔案夾拷貝過來就能運行。具體參考文檔參考文檔 .NET Core Application Deployment 

 

預設的project.json編譯出來的應用沒有包括跨平台,需要修改project.json檔案,需要在 project.json 加入 runtimes 節點 注釋掉 "type": "platform"

 

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {"hwapp":"1.0.0"},
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.0.0"
        }
      },
      "imports": "dnxcore50"
     }
    },

   "runtimes":{
      "win7-x64": { },
      "win7-x86": { },
      "osx.10.10-x64": { },
      "osx.10.11-x64": { },
      "ubuntu.14.04-x64":{ },
      "centos.7-x64":{}
   }

}

 

首先我們要dotnet restore,這裡我還原的時候有些久,大家耐心等待一下,因為我們設定4個平台。

dotnet publish -r centos.7-x64

 這個產生部署檔案的操作是可以跨平台的,在Windows上通過 dotnet publish -r centos.7-x64 命令產生Linux的部署檔案,然後將整個部署檔案夾上傳到Linux伺服器。

dotnet test

測試命令用來運行測試專案,使用配置的測試回合程式中定義的測試套件。你會瞭解更多有關此命令在本教程的後面的章節裡瞭解更詳細的內容。 

 

下次我們將深入瞭解一下到 project.json 檔案,並開始嘗試在如何構建更複雜的應用程式與新的.Net Core。

原文地址:http://www.mamicode.com/info-detail-1426076.html

NET Core 環境搭建和命令列CLI入門[轉]

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.