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

1

5

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

Gratis answered 18/11, 2020 at 16:53 Comment(5)
Have you done <DebugType>embed</DebugType> as mentioned in the .NET 5.0 Announcement?Moynahan
It looks like you need to use the debug version but can get the release version to create the pdb file. See : social.msdn.microsoft.com/Forums/en-US/…Wichman
@Moynahan wouldn't that be if I needed to debug the application? I only need to debug the plugin that is loaded dynamically and the PDB is there. But it's worth a try!Gratis
Have you tried copying mscordbi.dll from the win-x64 (or other target) directory into the exe directory? github.com/dotnet/runtime/issues/42927Azote
@Azote that's a good hint and probably would have worked. Though that breaks the intention of having a single file. So I'd consider that a workaround and not an answer. Thanks for the link though, that's exactly the root of the problem.Gratis
G
7

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.

Gratis answered 18/11, 2020 at 17:27 Comment(2)
Similarly on .net 6, found that on a system without the runtime installed, we had to either turn off PublishSingleFile, or set SelfContained to false (and install the .NET runtime) to be able to attach VS remote debugger successfully. Copying the mscordbi.dll next to the exe did not resolve the issue in our testing.Maribelmaribelle
Thanks this solved it. I couldn't debug my .NET6 singlefile exe with self-contained on. Without it, it just worked.Antiperspirant

© 2022 - 2024 — McMap. All rights reserved.