Skip to content
This repository was archived by the owner on May 21, 2026. It is now read-only.
This repository was archived by the owner on May 21, 2026. It is now read-only.

Avoid a first-chance exception in ConflictResolver.ResolveConflict #326

Closed

Description

@KirillOsenkov

I hit a first-chance BadImageFormatException a lot with this stack:

 	mscorlib.dll!System.Reflection.AssemblyName.GetAssemblyName Line 213	C#
 	Microsoft.Packaging.Tools.Tasks.dll!Microsoft.DotNet.Build.Tasks.FileUtilities.GetAssemblyVersion	Unknown
 	Microsoft.Packaging.Tools.Tasks.dll!Microsoft.DotNet.Build.Tasks.FileUtilities.TryGetAssemblyVersion	Unknown
 	Microsoft.Packaging.Tools.Tasks.dll!Microsoft.DotNet.Build.Tasks.ConflictItem.AssemblyVersion.get	Unknown
 	Microsoft.Packaging.Tools.Tasks.dll!Microsoft.DotNet.Build.Tasks.ConflictResolver.ResolveConflict	Unknown
>	Microsoft.Packaging.Tools.Tasks.dll!Microsoft.DotNet.Build.Tasks.ConflictResolver.ResolveConflicts	Unknown
 	Microsoft.Packaging.Tools.Tasks.dll!Microsoft.DotNet.Build.Tasks.HandlePackageFileConflicts.Execute	Unknown

This is because here we try to get the assembly version of any .dll even if it's not a managed assembly:
https://github.com/dotnet/standard/blob/master/Microsoft.Packaging.Tools/tasks/ConflictItem.cs#L56

I recommend using this simple code to first check whether a binary even represents a managed assembly:
https://github.com/KirillOsenkov/MetadataTools/blob/master/PEFile/IsManagedAssembly.cs

Only attempt to retrieve the assembly name after we've checked that the binary is indeed an assembly.

Activity

  1. ericstj commented on May 15, 2017

    @ericstj
    Member

    /cc @dsplaisted this code lives in the SDK as well.
    We do have a better implementation on core MSBuild: https://github.com/dotnet/standard/blob/master/Microsoft.Packaging.Tools/tasks/FileUtilities.netstandard.cs#L23

    On desktop though we just call GetAssemblyName. I suppose we could use the core implementation everywhere and it would avoid the exception.

  2. KirillOsenkov commented on May 15, 2017

    @KirillOsenkov
    MemberAuthor

    Yes, that method doesn't throw on native binaries. Please use it :)

  3. ericstj commented on May 15, 2017

    @ericstj
    Member

    Ported to the SDK: dotnet/sdk#1215

  4. ericstj commented on May 17, 2017

    @ericstj
    Member

    Curious what file you're hitting this on. We'd only be going down this path if you had a native file conflict in your package closure, which shouldn't be the case for most projects if they are up-to-date.

  5. KirillOsenkov commented on May 17, 2017

    @KirillOsenkov
    MemberAuthor

    Unfortunately I have no recollection of what I was doing and under what circumstances I've hit this. Sorry :)

  6. ericstj commented on May 18, 2017

    @ericstj
    Member

    We've decided that the tradeoffs for avoiding this first chance exception aren't worth it. I've opened https://github.com/dotnet/corefx/issues/19928 to track adding an API that can be used to do this better in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions