简体   繁体   中英

No symbols when attaching to .NET Core program (single file)

To better explain, I'll give some context.

I have an application that loads a library dynamically on runtime. The library is some sort of a plugin and the application is intended to test the plugin.

In order to be able to debug the plugin, I open VS Code with the plugin project and I attach the debugger to the application that is already running and has already loaded the plugin. The plugin assembly has been built and has a nice PDB file.

So the workflow follows: Application => load plugin => attach debugger

First of all, this works, even with the application published in release. That is because the plugin loaded is built in debug and so the PDB is there.

However, there's a scenario where I can't seem to attach properly or the symbols aren't loaded. And this is if I publish the application with the /p:PublishSingleFile=true flag. In this scenario a single file is generated and when I try to attach the debugger it just doesn't work.

Can anyone provide any insights on this? Or a possible solution?

It's worth saying that I'm using .NET 5

Cheers

Replying to my own question.

From .NET Core 3.0, using the flag /p:PublishSingleFile=true by default assumes --self-contained true . And it makes sense, if you're packing everything into one file, you probably want the .NET runtime there as well.

The thing with --self-contained is that it trims assemblies to reduce size.

The trim-self-contained deployment model is a specialized version of the self-contained deployment model that is optimized to reduce deployment size.

And with that, there are some risks that can lead the runtime to misbehave when it comes to reflection usage (like dynamically loading assemblies).

However, there is a risk that the build time analysis of the application can cause failures at runtime, due to not being able to reliably analyze various problematic code patterns (largely centered on reflection use).

You can read more here .

Disabling --self-contained resolves the issue and I was able to attach the debugger to the single file application. I did this by adding <SelfContained>false</SelfContained> to the project file.

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <SelfContained>false</SelfContained>
    <PublishSingleFile>true</PublishSingleFile>
  </PropertyGroup>
...

Hope this can help someone in the future.

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