Skip to content

[Single-File] DllImport fails to find native library #42772

@elinor-fung

Description

@elinor-fung

.NET SDK 5.0.100-rc.2.20473.8

  1. Include a native library in a .NET project's output:
    <ItemGroup>
      <None Include="...">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </None>
    </ItemGroup>
  2. Add a P/Invoke:
    static void Main(string[] args)
    {
        NativeLib.HelloWorld();
    }
    
    [DllImport("NativeLib")]
    private static extern void HelloWorld();
  3. Publish as single file: dotnet publish -p:PublishSingleFile=true ...
    • with different combinations of --self-contained and IncludeNativeLibrariesForSelfExtract
  4. Depending on publish configuration, application fails with DllNotFoundException
    • --self-contained true -p:IncludeNativeLibrariesForSelfExtract=false (default)
      • Linux: fails
      • Windows: works - directory with the single-file executable ends up on native search path because the native runtime bits are there
    • --self-contained false -p:IncludeNativeLibrariesForSelfExtract=false
      • Linux: fails
      • Windows: works - default library load logic will look next to the running executable
    • --self-contained true -p:IncludeNativeLibrariesForSelfExtract=true
      • Linux: fails
      • Windows: works - extraction directory ends up on native search path because the native runtime bits are extracted there
    • --self-contained false -p:IncludeNativeLibrariesForSelfExtract=true
      • Linux: fails
      • Windows: fails

This works with 3.1 single-file.
Setting IncludeAllContentForSelfExtract=true (with the fix in #42435) works.

cc @vitek-karas

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions