繁体   English   中英

.Net 框架的 Visual Studio 代码

[英]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 构建和调试项目,但它非常有限。

OmniSharpGitHub 页面(负责 C# 扩展)说:

C# 扩展支持有限的完整 .NET 框架调试。 它只能调试带有可移植 PDB 的64 位应用程序。

但是,即使在阅读了有关该主题的许多问题和讨论之后,我仍然有点不清楚必要的步骤是什么,因此我将在此处提供一些指南,其中包含我遵循的步骤并且对我有用,希望,也会为你工作。

  1. 必要的文件/文件夹是:

    一种。 .vscodelaunch.jsontasks.json

    bin\\Debug文件夹,用于您的 .exe 应用程序和您可能想要创建引用的程序集。

    d. <project>.csprojProgram.cs文件。

    e. 可选的批处理文件,我将在后面描述其用途。

  2. 安装MSBuild 15 (2017)

  3. <project>.csproj文件中:

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

    • 在第一PropertyGroup我们必须设定OutputTypeExe (默认可以是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>
  4. 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... } ,] }
  5. 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是我用前面的代码创建的批处理文件。

  6. 在此之后,您可能需要保存、关闭并重新打开文件(这多次为我解决了问题)。

  7. 将调试器中的配置设置为MyLauncher

    打印

  8. 使用绿色播放按钮运行您的代码; 它将调用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 框架上开始调试:

在 VS Code 中调试完整的 .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.

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