Aci*_*der 36 .net visual-studio-code
我很难弄清楚是否以及如何使用Visual Studio Code来开发和调试无法在.Net Core上运行的C#.Net程序的命令行/控制台/库,即它们需要.Net Framework.我需要访问没有.Net Core提供程序的Oracle,但它确实有一个Managed .Net Framework提供程序.我使用VS 2015/2017完成此任务,但如果我可以编写,构建和调试.Net Framework目标C#程序,我想切换到VS Code.我试过谷歌搜索,找不到任何东西.
小智 39
好吧,虽然我迟到了,但我发布了这个答案,因为它可能会帮助其他人面对这个问题,这对我来说非常累人.
首先,Visual Studio Code的最新更新确实支持.NET Framework的构建和调试项目,但它非常有限:
C#扩展支持有限的完整.NET框架调试.它只能使用便携式PDB调试64位应用程序.https://github.com/OmniSharp/omnisharp-vscode/wiki/Desktop-.NET-Framework
正如OmniSharp(负责C#扩展)github所说,该项目必须是带有便携式PDB的64位应用程序.
但是,即使在阅读了关于这个主题的许多问题和讨论之后,对我来说仍然有点不清楚是什么必要的步骤.因此,我将在这里向我们展示一个小指南,其中包含我所遵循的步骤,这对我有用,并且希望对您也有用.
1)必要的文件/文件夹是:
a).vscode与launch.json和tasks.json
b)exe应用程序的bin\Debug文件夹以及可能要创建引用的程序集
d).csproj和Program.cs文件
e)可选的批处理文件,其目的我将在后面描述
2)安装MSBuild 15
3)在.csproj文件中:
将Project Sdk ="Microsoft.NET.Sdk"更改为Project ToolsVersion ="15.0"xmlns ="http://schemas.microsoft.com/developer/msbuild/2003"
在第一个属性组中,我们必须将OutputType设置为Exe(默认值可能是dll),删除TargetFramework属性,替换为值为v4.6.1的TargetFrameworkVersion(例如.NET Framwork 4.6.1,例如可能为4.7) ,最后把运行时win-x64和win7-x64(以及编译器可能会抱怨的任何其他).第一组应如下所示:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<RuntimeIdentifiers>win-x64;win7-x64</RuntimeIdentifiers>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
一些注释:使用的条件表明这些属性仅在传递给编译器的配置为Debug且平台为"AnyCPU"时才适用,您可能希望插入具有不同值的其他条件,甚至根本不使用条件; 这里最重要的值是:PlatformTarget属性必须是x64,DebugType必须是可移植的 ; 输出路径设置为bin\Debug.
<ItemGroup>
<Compile Include="Program.cs" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
整个事情应该是这样的:
{
"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...
}
,]
}
Run Code Online (Sandbox Code Playgroud)
4)在launch.json中:
{
"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\""
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
5)在tasks.json中:
"(path)\MSBuild.exe" (project).csproj /t:Build /p:Configuration=Debug /p:Platform="AnyCPU"
Run Code Online (Sandbox Code Playgroud)
但还有另一种方法,使用.bat文件; 在我的情况下,MSBuild.exe的路径有空格,并且在任务运行时产生错误,因此我将以下代码放在.bat文件中(将记事本另存为name.bat):
"(path)\ MSBuild.exe"(项目).csproj/t:Build/p:Configuration = Debug/p:Platform ="AnyCPU"
然后将"mybuild"任务设置为:
{
"label": "mybuild",
"command":"build.bat",
"type":"shell",
"args":[]
}
Run Code Online (Sandbox Code Playgroud)
其中build.bat是我用前面的代码创建的批处理文件
6)在此之后,您可能必须保存,关闭并重新打开文件(这很多次修复了我的问题)
7)将调试器中的配置设置为MyLauncher:
8)使用绿色播放按钮运行代码; 它将调用MyLaunch,首先使用MSBuild 15构建您的项目,然后运行exe文件
就是这样.
希望它能帮到你.
如果您能提供任何反馈,那将是很棒的.
问候
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<RuntimeIdentifiers>win-x64;win7-x64</RuntimeIdentifiers>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
feO*_*O2x 11
我刚刚创建了一个简单的控制台应用程序,并自定义了csproj文件。之后,我可以将OmniSharp调试器附加到完整的.NET Framework应用程序。csproj文件如下所示:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net47</TargetFramework>
<PlatformTarget>x64</PlatformTarget>
<DebugType>portable</DebugType>
</PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
我只是遵循官方文档:我改为TargetFramework
在.NET 4.7、64 PlatformTarget
位和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}"
}
,]
}
Run Code Online (Sandbox Code Playgroud)
在这个文件中,我只是在两个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 到底是什么?》第一个答案的评论。
归档时间: |
|
查看次数: |
37433 次 |
最近记录: |