简体   繁体   中英

Cannot debug netstandard2.0 project in Visual Studio 2019

I'm building a project with the following in the CSPROJ file:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <TargetFramework>netstandard2.0</TargetFramework>
    <RootNamespace>basic_example</RootNamespace>
    <ImplicitUsings>disable</ImplicitUsings>
    <StartupObject>basic_example.LoopThroughInvalidFileChars</StartupObject>
      <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
  </PropertyGroup>
    
  <ItemGroup>
    <PackageReference Include="DocumentFormat.OpenXml" Version="2.14.0" />
  </ItemGroup>

</Project>

I'm interested in debugging a source file in this project using Visual Studio 2019. Here are the details:

在此处输入图像描述

When I start the project without debugging, it compiles and runs fine. However when I place a breakpoint in my source code and I try to start with debugging, it basically runs my program and never stops at the breakpoint.

In my output window in Visual Studio, the following message appears:

The target process exited without raising a CoreCLR started event. Ensure that the target > process is configured to use .NET Core. This may be expected if the target process did not > run on .NET Core. The program '[25444] basic-example.dll' has exited with code 0 (0x0).

However, I am intentionally setting the target framework to netstandard2.0 . Ie I would really like to debug it with the current project file.

Why won't Visual Studio allow me to debug this project?

Thanks to @LukeBriner's and @Dai's comments I was able to solve the problem.

As @LukeBriner mentions:

If you want to debug into it as a.netstandard library then just create a do.net core console app and call into the library in a normal way.

So that's what I did.

  • I had to rename all methods named Main in my class library to something else (I used Run ).
  • I added a console app to the solution adjacent to the class library and added a project reference to the class library.
  • I imported the class library in my console apps' Program.cs with a using statement. I called the class I wanted to debug in the Main method of Program.cs
  • I modified the project file of the console application to look like the following:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RootNamespace>call_basic_example</RootNamespace>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\openxml-exceptions\basic-example.csproj" />
  </ItemGroup>

</Project>
  • I modified the project file for the class library to look like the following:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <TargetFramework>netstandard2.0</TargetFramework>
    <RootNamespace>basic_example</RootNamespace>
    <ImplicitUsings>disable</ImplicitUsings>
      <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
  </PropertyGroup>
    
  <ItemGroup>
    <PackageReference Include="DocumentFormat.OpenXml" Version="2.14.0" />
  </ItemGroup>

</Project>

I was then finally able to debug into whichever method I wanted in the class library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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