做爲曾經的Windows客戶端程序員,對微軟的技術實力仍是至關佩服的。不過隨着互聯網的大發展,微軟長期以來的封閉策略不受廣大程序員的待見。看着Java、Python、PHP等語言在服務端受到熱捧,Go, Elixer,Rust等後起之秀也廣受關注,Node更時激發了各類奇怪設計的Javascript煥發出洪荒之力,大有一統先後端的氣勢。微軟坐不住了,因而開源了.Net,通過近2年的努力開發,.Net Core 1.0 版本發佈。讓咱們拋開偏見,試一下微軟最大尺度的開源做品吧。程序員
安裝很簡單,各個平臺的下載地址能夠在主頁找到:https://www.microsoft.com/net/core 由於本人不想使用宇宙第一大IDE來安裝.NET Core, 全部選擇單獨安裝包,https://go.microsoft.com/fwlink/?LinkID=827524 下載,運行,一下搞定。打開命令行,輸入dotnet, 結果以下:web
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 versi on. 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
說明安裝好了。shell
在命令行下,輸入以下命令:json
D:\devs\dot>mkdir core D:\devs\dot>cd core D:\devs\dot\core>mkdir start D:\devs\dot\core>cd start D:\devs\dot\core\start>dotnet new Welcome to .NET Core! --------------------- Learn more about .NET Core @ https://aka.ms/dotnet-docs. Use dotnet --help to se e available commands or go to https://aka.ms/dotnet-cli-docs. Telemetry -------------- The .NET Core tools collect usage data in order to improve your experience. The data is anonymous and does not include commandline arguments. The data is collec ted by Microsoft and shared with the community. You can opt out of telemetry by setting a DOTNET_CLI_TELEMETRY_OPTOUT environmen t variable to 1 using your favorite shell. You can read more about .NET Core tools telemetry @ https://aka.ms/dotnet-cli-te lemetry. Configuring... ------------------- A command is running to initially populate your local package cache, to improve restore speed and enable offline access. This command will take up to a minute t o complete and will only happen once. Decompressing 100% 2948 ms Expanding 100% 17535 ms Created new C# project in D:\devs\dot\core\start.
程序建立完畢。後端
剛剛的dotnet new命令建立了兩個文件:Program.cs和project.json。其中,Program.cs是C#程序文件,project.json是工程配置文件,和其餘語言的配置文件相似,包含程序的版本和依賴信息。要編譯程序,首先須要把這些依賴都給解決了,因此須要運行 dotnet restore 命令,下載相應的依賴。app
D:\devs\dot\core\start>dotnet restore log : Restoring packages for D:\devs\dot\core\start\project.json... log : Writing lock file to disk. Path: D:\devs\dot\core\start\project.lock.json log : D:\devs\dot\core\start\project.json log : Restore completed in 2445ms.
而後使用命令 dotnet run 編譯並運行ui
D:\devs\dot\core\start>dotnet run Project start (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing Compiling start for .NETCoreApp,Version=v1.0 Compilation succeeded. 0 Warning(s) 0 Error(s) Time elapsed 00:00:01.7660521 Hello World!
看到熟悉的Hello World! 說明運行成功。lua
至此,高水平程序員已經能夠開始用記事本開心的編寫程序了。做爲懶惰的程序員,固然沒法忍受這樣費力的方式啦。對Visual Studio Code作一番配置,使其做爲一個C# 輕量IDE。spa
按F5快捷鍵就能夠啓動調試了,第一次調試,會生成一個.vscode文件夾,在文件夾下面建立兩個文件: tasks.json 和 launch.json,task.json裏包含運行調試的各類命令,默認是一個build命令。這個命令在luanch.json裏的preLaunchTask": "build"被調用。launch.json包含各個調試配置,默認生成三種啓動調試的方式:.net
默認是console方式啓動調試。正常狀況,啓動調試的配置文件會把program配置改爲項目的目錄,若是您的program仍是"program": "${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>",這樣的形勢,說明沒有自動變換路徑,手動改一下,改爲 "program": "${workspaceRoot}/bin/Debug/netcoreapp1.0/start.dll",其中 start.dll是項目名,默認與文件夾同名。
若是在程序中加了斷點,那麼這個時候就能停下來。
程序寫好了,須要發佈大到其餘機器運行,.Net Core 有兩種發佈形式:
D:\publish>dotnet start.dll Hello World!3
運行成功。若是要編一個release, 加上release選項:dotnet publish -c release
{ "version": "1.0.0-*", "buildOptions": { "debugType": "portable", "emitEntryPoint": true }, "dependencies": {}, "frameworks": { "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { //"type": "platform", "version": "1.0.1" } }, "imports": "dnxcore50" } }, "runtimes": { "win10-x64": {}, "osx.10.10-x64": {} } }
運行命令
dotnet restore dotnet build -r win10-x64
debug目錄下會出現一個win10-x64文件夾,裏面有久違的exe文件啦。