简体   繁体   English

.Net 框架的 Visual Studio 代码

[英]Visual Studio Code for .Net Framework

I am having difficulty figuring out if and how I can use Visual Studio Code to develop and debug command-line/console/libraries of C#.Net programs which can not run on .Net Core , ie they require .Net Framework .我很难弄清楚是否以及如何使用 Visual Studio Code来开发和调试无法在 .Net Core 上运行的 C#.Net 程序的命令行/控制台/库,即它们需要 .Net Framework I need to access Oracle which do not have a .Net Core provider but it does have a Managed .Net Framework provider.我需要访问没有 .Net Core 提供程序但它有托管 .Net Framework 提供程序的 Oracle。 I use VS 2015/2017 for this task but would like to switch to VS Code if I could code, build and debug .Net Framework target C# programs.我将 VS 2015/2017 用于此任务,但如果我可以编码、构建和调试 .Net Framework 目标 C# 程序,我想切换到 VS Code。 I have tried Google search and could not find anything.我试过谷歌搜索,找不到任何东西。

First thing, the more recent updates for Visual Studio Code do support building and debugging projects for the .NET Framework, but it is very limited.首先,Visual Studio Code 的最新更新确实支持为 .NET Framework 构建和调试项目,但它非常有限。

The GitHub page for OmniSharp (responsible for the C# extension) says that: OmniSharpGitHub 页面(负责 C# 扩展)说:

The C# extension supports limited full .NET framework debugging. C# 扩展支持有限的完整 .NET 框架调试。 It can only debug 64-bit applications with portable PDBs .它只能调试带有可移植 PDB 的64 位应用程序。

But, even after reading many issues and discussions about this topic, it remained a little bit unclear for me what the necessary steps were, so I will expose here a little guide with the steps that I have followed and that worked for me, and hopefully, will also work for you.但是,即使在阅读了有关该主题的许多问题和讨论之后,我仍然有点不清楚必要的步骤是什么,因此我将在此处提供一些指南,其中包含我遵循的步骤并且对我有用,希望,也会为你工作。

  1. The necessary files/folders are:必要的文件/文件夹是:

    a.一种。 .vscode with launch.json and tasks.json . .vscodelaunch.jsontasks.json

    b.bin\\Debug folder for your .exe application and the assemblies you might want to create a reference to. bin\\Debug文件夹,用于您的 .exe 应用程序和您可能想要创建引用的程序集。

    d. d. the <project>.csproj and Program.cs files. <project>.csprojProgram.cs文件。

    e. e. optionally a batch file, whose purpose I will describe later.可选的批处理文件,我将在后面描述其用途。

  2. Install MSBuild 15 (2017) .安装MSBuild 15 (2017)

  3. In the <project>.csproj file:<project>.csproj文件中:

    • change the Project Sdk="Microsoft.NET.Sdk" to Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" .Project Sdk="Microsoft.NET.Sdk"更改为Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"

    • in the first PropertyGroup we must set the OutputType to Exe (the default may be dll ), remove the TargetFramework property, replacing with TargetFrameworkVersion with value v4.6.1 (example for .NET Framwork 4.6.1, it may be 4.7 for instance), and finally put the runtimes win-x64 and win7-x64 (and any other that the compiler may complain).在第一PropertyGroup我们必须设定OutputTypeExe (默认可以是dll ),取出TargetFramework属性,替换与TargetFrameworkVersion与值v4.6.1 (例如用于.NET Framwork 4.6.1,它可以是4.7实例),最后放置运行时 win-x64 和 win7-x64(以及编译器可能会抱怨的任何其他运行时)。 This first PropertyGroup should look like this:第一个PropertyGroup应如下所示:

       <PropertyGroup> <OutputType>Exe</OutputType> <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> <RuntimeIdentifiers>win-x64;win7-x64</RuntimeIdentifiers> </PropertyGroup>
    • set another PropertyGroup` with the following items:使用以下项目设置另一个 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>

    Some comments: the condition used signals that these properties only apply when the configuration passed to the compiler is Debug and the Platform is "AnyCPU", you might want to insert other conditions with different values, or even don't use a condition at all;一些评论:使用的条件表明这些属性仅在传递给编译器的配置为 Debug 且平台为“AnyCPU”时才适用,您可能想要插入具有不同值的其他条件,甚至根本不使用条件; the most import values here are: The PlatformTarget property must be x64 and the DebugType must be portable ;这里最重要的值是: PlatformTarget属性必须x64并且DebugType必须是可移植的 the output path is set to bin\\Debug.输出路径设置为 bin\\Debug。

    • As we are not using the Microsoft SDK we must include the Program.cs , so that the compiler can find it:由于我们没有使用 Microsoft SDK,我们必须包含Program.cs ,以便编译器可以找到它:

       <ItemGroup> <Compile Include="Program.cs" /> </ItemGroup>
    • create the necessary references to your project, for example:创建对项目的必要引用,例如:

       <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>
    • finally import the following tools (make sure you follow the order exposed here, placing this in the beginning for instance you generate an error)最后导入以下工具(确保您遵循此处公开的顺序,将其放在开头,例如您会生成错误)

    The whole thing should look like this:整个事情应该是这样的:

     <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>
  4. In the launch.json :launch.json

    • Create a new configuration(eg MyLauncher ), whose type must be clr , and that points to your program;创建一个新的配置(例如MyLauncher ),其类型必须是 clr ,并且指向您的程序; the preLaunchTask will be set to a manually configured one ( "mybuild" , for instance) that will be specified in the tasks.json ; preLaunchTask将被设置为一个手动配置的(例如"mybuild" ),它将在tasks.json指定; an example of the configuration is:配置的一个例子是:

       { "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... } ,] }
  5. In the tasks.json :tasks.json

    • Create a task "mybuild" with the commands to build your project.使用命令创建一个任务"mybuild"来构建您的项目。

    • We will use the MSBuild 15 here (don't use the dotnet build - at least it has not worked for me).我们将在这里使用 MSBuild 15(不要使用 dotnet 构建 - 至少它对我不起作用)。

    • You can directly point to the (path)\\MSBuild.exe (or msbuild.exe , if it is in the %PATH% ) file with the arguments to build the project.您可以使用参数直接指向(path)\\MSBuild.exe (或msbuild.exe ,如果它在%PATH% )文件以构建项目。 One example is shown below, note that I've set the Configuration to Debug and the platform to AnyCPU, matching the condition Ive set in the .csproj file, also note that the backslashes in \\"AnyCPU\\" are because of the use of the quotation marks.一个例子如下所示,注意我把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\\"" ] } ] }
    • but there is another way, using the .bat file;但还有另一种方法,使用.bat文件; in my case the path to the MSBuild.exe had spaces and that was generating an error when the task run, so that I've put the following code in a .bat file (save notepad as name.bat ):在我的情况下, MSBuild.exe的路径有空格,并且在任务运行时生成错误,因此我将以下代码放在.bat文件中(将记事本另存为name.bat ):

       "(path)\\MSBuild.exe" (project).csproj /t:Build /p:Configuration=Debug /p:Platform="AnyCPU"

      and then set the "mybuild" task to:然后将"mybuild"任务设置为:

       { "label": "mybuild", "command":"build.bat", "type":"shell", "args":[] }

      Where build.bat is the batch file I have created with the previous code.其中build.bat是我用前面的代码创建的批处理文件。

  6. After this, you might have to save, close and reopen the files (this many times fixes problems for me).在此之后,您可能需要保存、关闭并重新打开文件(这多次为我解决了问题)。

  7. Set your configuration in the debugger to MyLauncher :将调试器中的配置设置为MyLauncher

    打印

  8. Run your code with the green play button;使用绿色播放按钮运行您的代码; it will call the MyLauncher , that first will build your project with MSBuild 15 and then run the exe file它将调用MyLauncher ,首先将使用 MSBuild 15 构建您的项目,然后运行 ​​exe 文件

So that was it.就是这样。

Here are some references:以下是一些参考:

I just created a simple console application and customized the csproj file.我刚刚创建了一个简单的控制台应用程序并自定义了 csproj 文件。 Afterwards, I could attach the OmniSharp debugger to a full .NET framework application.之后,我可以将 OmniSharp 调试器附加到完整的 .NET 框架应用程序。 The csproj file looks like this: csproj 文件如下所示:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net47</TargetFramework>
    <PlatformTarget>x64</PlatformTarget>
    <DebugType>portable</DebugType>
  </PropertyGroup>

</Project>

I just followed the official documentation : I changed TargetFramework to run on .NET 4.7, the PlatformTarget to 64 bit and the DebugType to portable.我只是按照官方文档进行操作:我将TargetFramework更改为在 .NET 4.7 上运行,将PlatformTarget更改为 64 位,将DebugType为可移植的。

Furthermore, I updated launch.json:此外,我更新了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}"
        }
    ,]
}

In this file, I just changed type to clr in both JSON objects, and targeted program to the exe file.在这个文件中,我只是在两个 JSON 对象中将type更改为clr ,并将目标program更改为 exe 文件。

Afterwards, I could set a break point and simply press F5 to start debugging on full .NET framework:之后,我可以设置一个断点,然后只需按 F5 即可在完整的 .NET 框架上开始调试:

在 VS Code 中调试完整的 .NET 框架

https://code.visualstudio.com/docs/languages/csharp https://code.visualstudio.com/docs/languages/csharp

Quote:报价:

Note: VS Code does not support debugging applications running on the Desktop .NET Framework .注意:VS Code 不支持调试在桌面 .NET Framework上运行的应用程序。

It looks like Visual Studio 'full-fat' IDE remains a requirement for .Net Framework.看起来 Visual Studio 'full-fat' IDE 仍然是 .Net Framework 的要求。 Major pity.大可惜。

Unfortunately, it doesn't feature intellisense for C/C++, only syntax highlighting: code.visualstudio.com/docs/languages EDIT: no debugger integration for C/C++ either.不幸的是,它没有 C/C++ 的智能感知功能,只有语法突出显示:code.visualstudio.com/docs/languages 编辑:C/C++ 也没有调试器集成。 The git integration is really nice though!不过 git 集成真的很好! Seems more designed for web applications, the debugger works for node.js似乎更适合 Web 应用程序,调试器适用于 node.js

Whilst this does not specify C#, it stands to reason that the same standards apply (which is that there is no debugger and no compile functionality).虽然这没有指定 C#,但有理由应用相同的标准(即没有调试器和编译功能)。

Quote taken from comment on first answer of What exactly is Visual Studio Code?引自对什么是 Visual Studio Code 的第一个答案的评论

To start with, install Build Tools for Visual Studio 2019首先,安装 Visual Studio 2019 的构建工具

To your VS Code workspace add .vscode folder and add tasks.json to build your project.向您的 VS Code 工作区添加 .vscode 文件夹并添加 tasks.json 以构建您的项目。 Use below a sample of tasks.json can be used mostly for any .Net Framework project.使用下面的 tasks.json 示例可主要用于任何 .Net Framework 项目。

 { "version": "2.0.0", "command": "msbuild", "args": [ "/property:GenerateFullPaths=true" ], "tasks": [{ "label": "build", "problemMatcher": "$msCompile" }] }

Next step to make F5 work with VS Code.下一步使 F5 与 VS Code 一起工作。 In order to make it work add launch.json in .vscode folder为了使其工作在 .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}" } ] }

Note: Please change "program" and "cwd" for your application注意:请为您的应用程序更改“程序”和“cwd”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM