.Net 框架的 Visual Studio 代码 [英] Visual Studio Code for .Net Framework

查看:15
本文介绍了.Net 框架的 Visual Studio 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难确定是否以及如何使用 Visual Studio 代码 来开发和调试无法在 .Net 上运行的 C#.Net 程序的命令行/控制台/库.Net Core,即它们需要 .Net Framework.我需要访问没有 .Net Core 提供程序但它有托管 .Net Framework 提供程序的 Oracle.我将 VS 2015/2017 用于此任务,但如果我可以编码、构建和调试 .Net Framework 目标 C# 程序,我想切换到 VS Code.我尝试过 Google 搜索,但找不到任何内容.

解决方案

首先,Visual Studio Code 的最新更新确实支持为 .NET Framework 构建和调试项目,但它非常有限.

  • 使用绿色播放按钮运行您的代码;它将调用 MyLauncher,首先使用 MSBuild 15 构建您的项目,然后运行 ​​exe 文件

  • 原来如此.

    以下是一些参考资料:

    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, i.e. they require .Net Framework. I need to access Oracle which do not have a .Net Core provider but it does have a Managed .Net Framework provider. 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. 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.

    The GitHub page for OmniSharp (responsible for the C# extension) says that:

    The C# extension supports limited full .NET framework debugging. It can only debug 64-bit applications with portable PDBs.

    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.

      b. binDebug folder for your .exe application and the assemblies you might want to create a reference to.

      d. the <project>.csproj and Program.cs files.

      e. optionally a batch file, whose purpose I will describe later.

    2. Install MSBuild 15 (2017).

    3. In the <project>.csproj file:

      • change the Project Sdk="Microsoft.NET.Sdk" to 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). This first PropertyGroup should look like this:

        <PropertyGroup>
           <OutputType>Exe</OutputType>
           <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
           <RuntimeIdentifiers>win-x64;win7-x64</RuntimeIdentifiers>
        </PropertyGroup>
        

      • set another PropertyGroup` with the following items:

        <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
          <PlatformTarget>x64</PlatformTarget>
          <DebugSymbols>true</DebugSymbols>
          <DebugType>portable</DebugType>
          <Optimize>false</Optimize>
          <OutputPath>binDebug</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; the most import values here are: The PlatformTarget property must be x64 and the DebugType must be portable; the output path is set to binDebug.

      • As we are not using the Microsoft SDK we must include the Program.cs, so that the compiler can find it:

        <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>binDebug</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>binRelease</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:

      • Create a new configuration(e.g. MyLauncher), whose type must be clr, and that points to your program; the preLaunchTask will be set to a manually configured one ("mybuild", for instance) that will be specified in the 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:

      • Create a task "mybuild" with the commands to build your project.

      • We will use the MSBuild 15 here (don't use the dotnet build - at least it has not worked for me).

      • 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. 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.

        {
            "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; 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):

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

        and then set the "mybuild" task to:

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

        Where build.bat is the batch file I have created with the previous code.

    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:

    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

    So that was it.

    Here are some references:

    这篇关于.Net 框架的 Visual Studio 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆