Skip to content

Single-File host does not correctly redirect hostpolicy PInvokes #39907

@vitek-karas

Description

@vitek-karas

There's a managed component AssemblyDependencyResolver which is implemented by PInvoking into hostpolicy. Since in the new single-file the hostpolicy doesn't exist as a separate module, we need to redirect the PInvokes to the main module, just like we do for other statically linked libraries.

Note that this needs to work on Windows as well - which is different from the case we already implement which is Linux only.

Repro:

using System.Runtime.Loader;

namespace app
{
    class Program
    {
        static void Main(string[] args)
        {
            AssemblyDependencyResolver resolver = new AssemblyDependencyResolver("path.dll");
        }
    }
}

Run as a single-file self-contained console app. i.e. dotnet publish -r win-x64 /p:PublishSingleFile=true. The result is:

Unhandled exception. System.InvalidOperationException: Cannot load hostpolicy library. AssemblyDependencyResolver is currently only supported if the runtime is hosted through hostpolicy library.
 ---> System.DllNotFoundException: Dll was not found.
   at Interop.HostPolicy.corehost_set_error_writer(IntPtr errorWriter)
   at System.Runtime.Loader.AssemblyDependencyResolver..ctor(String componentAssemblyPath)

It will work if published as FDD single-file i.e. dotnet publish -r win-x64 --self-contained false /p:PublishSingleFile=true - this will now fail with error about not finding path.dll.

I verified that it behaves the same on Linux with the latest RC from installers.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions