[英]Visual Studio Code for .Net Framework
我很難弄清楚是否以及如何使用 Visual Studio Code來開發和調試無法在 .Net Core 上運行的 C#.Net 程序的命令行/控制台/庫,即它們需要 .Net Framework 。 我需要訪問沒有 .Net Core 提供程序但它有托管 .Net Framework 提供程序的 Oracle。 我將 VS 2015/2017 用於此任務,但如果我可以編碼、構建和調試 .Net Framework 目標 C# 程序,我想切換到 VS Code。 我試過谷歌搜索,找不到任何東西。
首先,Visual Studio Code 的最新更新確實支持為 .NET Framework 構建和調試項目,但它非常有限。
OmniSharp的GitHub 頁面(負責 C# 擴展)說:
C# 擴展支持有限的完整 .NET 框架調試。 它只能調試帶有可移植 PDB 的64 位應用程序。
但是,即使在閱讀了有關該主題的許多問題和討論之后,我仍然有點不清楚必要的步驟是什么,因此我將在此處提供一些指南,其中包含我遵循的步驟並且對我有用,希望,也會為你工作。
必要的文件/文件夾是:
一種。 .vscode
與launch.json
和tasks.json
。
灣 bin\\Debug
文件夾,用於您的 .exe 應用程序和您可能想要創建引用的程序集。
d. <project>.csproj
和Program.cs
文件。
e. 可選的批處理文件,我將在后面描述其用途。
在<project>.csproj
文件中:
將Project Sdk="Microsoft.NET.Sdk"
更改為Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
。
在第一PropertyGroup
我們必須設定OutputType
到Exe
(默認可以是dll
),取出TargetFramework
屬性,替換與TargetFrameworkVersion
與值v4.6.1
(例如用於.NET Framwork 4.6.1,它可以是4.7實例),最后放置運行時 win-x64 和 win7-x64(以及編譯器可能會抱怨的任何其他運行時)。 第一個PropertyGroup
應如下所示:
<PropertyGroup> <OutputType>Exe</OutputType> <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> <RuntimeIdentifiers>win-x64;win7-x64</RuntimeIdentifiers> </PropertyGroup>
使用以下項目設置另一個 PropertyGroup`:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PlatformTarget>x64</PlatformTarget> <DebugSymbols>true</DebugSymbols> <DebugType>portable</DebugType> <Optimize>false</Optimize> <OutputPath>bin\\Debug\\</OutputPath> <DefineConstants>DEBUG;TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup>
一些評論:使用的條件表明這些屬性僅在傳遞給編譯器的配置為 Debug 且平台為“AnyCPU”時才適用,您可能想要插入具有不同值的其他條件,甚至根本不使用條件; 這里最重要的值是: PlatformTarget屬性必須是x64並且DebugType必須是可移植的; 輸出路徑設置為 bin\\Debug。
由於我們沒有使用 Microsoft SDK,我們必須包含Program.cs
,以便編譯器可以找到它:
<ItemGroup> <Compile Include="Program.cs" /> </ItemGroup>
創建對項目的必要引用,例如:
<ItemGroup> <Reference Include="mscorlib" /> <Reference Include="System.Core" /> <Reference Include="System.Windows" /> <Reference Include="System.ServiceModel" /> <Reference Include="System.Net" /> <Reference Include="System.Xml" /> <Reference Include="System" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Data" /> <Reference Include="System.Net.Http" /> </ItemGroup>
最后導入以下工具(確保您遵循此處公開的順序,將其放在開頭,例如您會生成錯誤)
整個事情應該是這樣的:
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> <RuntimeIdentifiers>win-x64;win7-x64</RuntimeIdentifiers> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PlatformTarget>x64</PlatformTarget> <DebugSymbols>true</DebugSymbols> <DebugType>portable</DebugType> <Optimize>false</Optimize> <OutputPath>bin\\Debug\\</OutputPath> <DefineConstants>DEBUG;TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PlatformTarget>x64</PlatformTarget> <DebugType>portable</DebugType> <Optimize>true</Optimize> <OutputPath>bin\\Release\\</OutputPath> <DefineConstants>TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup> <ItemGroup> <Compile Include="Program.cs" /> </ItemGroup> <ItemGroup> <Reference Include="mscorlib" /> <Reference Include="System.Core" /> <Reference Include="System.Windows" /> <Reference Include="System.ServiceModel" /> <Reference Include="System.Net" /> <Reference Include="System.Xml" /> <Reference Include="System" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Data" /> <Reference Include="System.Net.Http" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\\Microsoft.CSharp.targets" /> </Project>
在launch.json
:
創建一個新的配置(例如MyLauncher
),其類型必須是 clr ,並且指向您的程序; preLaunchTask
將被設置為一個手動配置的(例如"mybuild"
),它將在tasks.json
指定; 配置的一個例子是:
{ "version": "0.2.0", "configurations": [ { "name": "MyLauncher", "type":"clr", "request": "launch", "preLaunchTask": "mybuild", "program": "${workspaceFolder}/bin/Debug/<project>.exe", "args":[], "console": "internalConsole", "stopAtEntry": false, "internalConsoleOptions": "openOnSessionStart" }, { other configurations... } ,] }
在tasks.json
:
使用命令創建一個任務"mybuild"
來構建您的項目。
我們將在這里使用 MSBuild 15(不要使用 dotnet 構建 - 至少它對我不起作用)。
您可以使用參數直接指向(path)\\MSBuild.exe
(或msbuild.exe
,如果它在%PATH%
)文件以構建項目。 一個例子如下所示,注意我把Configuration設置為Debug,平台設置為AnyCPU,與我在.csproj
文件中設置的條件相匹配,還要注意\\"AnyCPU\\"
中的反斜杠是因為使用了引號。
{ "version": "2.0.0", "tasks": [ { "label": "mybuild", "command":"<path to msbuild>\\MSBuild.exe", "type":"shell", "args":[ "<project>.csproj", "/t:Build", "/p:Configuration=Debug", "/p:Platform=\\"AnyCPU\\"" ] } ] }
但還有另一種方法,使用.bat
文件; 在我的情況下, MSBuild.exe
的路徑有空格,並且在任務運行時生成錯誤,因此我將以下代碼放在.bat
文件中(將記事本另存為name.bat
):
"(path)\\MSBuild.exe" (project).csproj /t:Build /p:Configuration=Debug /p:Platform="AnyCPU"
然后將"mybuild"
任務設置為:
{ "label": "mybuild", "command":"build.bat", "type":"shell", "args":[] }
其中build.bat
是我用前面的代碼創建的批處理文件。
在此之后,您可能需要保存、關閉並重新打開文件(這多次為我解決了問題)。
將調試器中的配置設置為MyLauncher
:
使用綠色播放按鈕運行您的代碼; 它將調用MyLauncher
,首先將使用 MSBuild 15 構建您的項目,然后運行 exe 文件
就是這樣。
以下是一些參考:
我剛剛創建了一個簡單的控制台應用程序並自定義了 csproj 文件。 之后,我可以將 OmniSharp 調試器附加到完整的 .NET 框架應用程序。 csproj 文件如下所示:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net47</TargetFramework>
<PlatformTarget>x64</PlatformTarget>
<DebugType>portable</DebugType>
</PropertyGroup>
</Project>
我只是按照官方文檔進行操作:我將TargetFramework
更改為在 .NET 4.7 上運行,將PlatformTarget
更改為 64 位,將DebugType
為可移植的。
此外,我更新了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 Launch (console)",
"type": "clr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net47/FullNetInVsCode.exe",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
"console": "internalConsole",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Attach",
"type": "clr",
"request": "attach",
"processId": "${command:pickProcess}"
}
,]
}
在這個文件中,我只是在兩個 JSON 對象中將type
更改為clr
,並將目標program
更改為 exe 文件。
之后,我可以設置一個斷點,然后只需按 F5 即可在完整的 .NET 框架上開始調試:
https://code.visualstudio.com/docs/languages/csharp
報價:
注意:VS Code 不支持調試在桌面 .NET Framework上運行的應用程序。
看起來 Visual Studio 'full-fat' IDE 仍然是 .Net Framework 的要求。 大可惜。
不幸的是,它沒有 C/C++ 的智能感知功能,只有語法突出顯示:code.visualstudio.com/docs/languages 編輯:C/C++ 也沒有調試器集成。 不過 git 集成真的很好! 似乎更適合 Web 應用程序,調試器適用於 node.js
雖然這沒有指定 C#,但有理由應用相同的標准(即沒有調試器和編譯功能)。
引自對什么是 Visual Studio Code 的第一個答案的評論?
首先,安裝 Visual Studio 2019 的構建工具
向您的 VS Code 工作區添加 .vscode 文件夾並添加 tasks.json 以構建您的項目。 使用下面的 tasks.json 示例可主要用於任何 .Net Framework 項目。
{ "version": "2.0.0", "command": "msbuild", "args": [ "/property:GenerateFullPaths=true" ], "tasks": [{ "label": "build", "problemMatcher": "$msCompile" }] }
下一步使 F5 與 VS Code 一起工作。 為了使其工作在 .vscode 文件夾中添加 launch.json
{ "version": "0.2.0", "configurations": [ { "name": ".NET Framework Launch", "type": "clr", "request": "launch", "preLaunchTask": "build", "program": "demo.console.exe", "args": [], "cwd": "${workspaceFolder}\\\\demo.console\\\\bin\\\\debug\\\\net461\\\\", "stopAtEntry": false }, { "name": ".NET Framework", "type": "clr", "request": "attach", "processId": "${command:pickProcess}" } ] }
注意:請為您的應用程序更改“程序”和“cwd”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.