visual studio code .net 開發
Visual Studio確實是相當好用,各種簡化操作什麼的簡直不要太舒服。但其容量太大,有時不是很方便,所以今天簡單介紹一下另一個工具--Visual Studio Code.
雖然相比於老大哥Visual Studio,VS Code有很多功能不完善,但它也更靈活輕便。並且VS Code還在不斷的更新當中,目前的最新版本是18年11月更新的1.30版本,包含了 多行搜尋改進 等內容。
下面以.Net開發為例:
不同於Visual Studio,在VS Code上進行.Net開發你需要安裝一些外掛,點選左側邊欄箭頭這個位置搜尋
安裝外掛 C# (C# for Visual Studio Code (powered by OmniSharp))
-
Lightweight development tools for .NET Core.
-
-
Great C# editing support, including Syntax Highlighting, IntelliSense, Go to Definition, Find
-
All References, etc.
-
Debugging support for .NET Core (CoreCLR). NOTE: Mono debugging is not supported. Desktop CLR debugging has limited support.
-
Support for project.json and csproj projects on Windows, macOS and Linux.
安裝外掛 NuGet Package Manager (推薦,方便搜尋,安裝Nuget包) (推薦)
-
Search the NuGet package repository for packages using either (partial or full) package name or another search term.
-
Add PackageReference dependencies to your .NET Core 1.1+ .csproj or .fsproj files from Visual Studio Code's Command Palette.
-
Remove installed packages from your project's .csproj or .fsproj files via Visual Studio Code's Command Palette.
-
Handles workspaces with multiple .csproj or .fsproj files as well as workspaces with single .csproj/.fsproj files.
-
-
-
For example:
-
-
-
Ctrl + P Then Shift + > Choose "Nuget Package Manager: Add Package". Then Input the keyword about the Nuget Package. Finally, Choose the project you want to add the Nuget package.
VSCode 安裝外掛後一般需要重新啟用以啟用外掛。由於VSCode本身不斷更新,對於某些版本的VSCode可能需要重啟應用,才能啟用外掛。
在VSCode中執行除錯前,需要在.vscode路徑下額外配置兩個檔案 launch.json 和 tasks.json, 來告訴vscode如何啟動專案。
Launch.json:
{ // Use IntelliSense to find out which attributes exist for C# debugging // Use hover for the description of the existing attributes // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md "version": "0.2.0", "configurations": [ { "name": ".NET Core Launch (web)", "type": "coreclr", "request": "launch", "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. "program": "${workspaceRoot}/MyABP.Web/bin/Debug/netcoreapp2.1/MyABP.Web.dll", "args": [], "cwd": "${workspaceRoot}/MyABP.Web", "stopAtEntry": false, "internalConsoleOptions": "openOnSessionStart", "launchBrowser": { "enabled": true, "args": "${auto-detect-url}", "windows": { "command": "cmd.exe", "args": "/C start ${auto-detect-url}" }, "osx": { "command": "open" }, "linux": { "command": "xdg-open" } }, "env": { "ASPNETCORE_ENVIRONMENT": "Development" }, "sourceFileMap": { "/Views": "${workspaceRoot}/Views" } }, { "name": ".NET Core Attach", "type": "coreclr", "request": "attach", "processId": "${command:pickProcess}" } ] }
如果你第一次執行某個專案,你需要確保所有引用的內容都存在,如上面的MyAbp.Web.dll檔案.另外,這些配置不是固定不變的,你可以根據你的需要來進行不同的配置。
如上面
"preLaunchTask": "build" 指定了你的專案在launch之前要先進行build操作。
又如
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
指定了你要在“哪個環境”下啟動你的專案。(實際上一個專案會有多種環境的配置,舉個例子 appsetings.Development.json 和 appseting.QA.json用於區分不同環境的配置,如果發現在QA環境出現了問題本地不能重現時,自然需要切換到目標環境來進行除錯)
Tasks.json:
{ "version": "0.1.0", "command": "dotnet", "isShellCommand": true, "args": [], "tasks": [ { "taskName": "build", "args": [ "${workspaceRoot}/MyABP.Web/MyABP.Web.csproj" ], "isBuildCommand": true, "problemMatcher": "$msCompile" } ] }
這裡可以配置一下Task,如上面的
"preLaunchTask": "build" 具體的任務流程即在這裡配置。
這裡除去筆者使用的Web專案的配置外,當然還可以有多種其他專案應用的配置,如
這些配置完成之後,點選如圖所示這個位置進入除錯面板,然後點選上面的綠色三角就可以開始你的除錯啦
/ **************************************************分割線*************************************************************/
vscode下常用DotNet命令
dotnet所使用的命令都可以使用 --help的方式來檢視更詳細的用法。
如dotnet --help
在專案開發過程中,常用的命令有
dotnet new
用於新建內容,dotnet提供了許多模板,如Console Application, Class Library等等。
使用dotnet new時需要注意新建的專案是基於.Net Framework還是基於.NetCore.
可以使用 -f 來指定.
dotnet restore
主要是尋找當前目錄下的專案檔案(project.json),然後利用NuGet庫還原整個專案的依賴庫,然後遍歷每個目錄,生成專案檔案,繼續還原該專案檔案中的依賴項 --CSDN yangzhenping
以下命令不解釋了,可以使用 --help 檢視具體用法
dotnet build
dotnet run
dotnet publish