I have a simple .NET Core console application that targets netcoreapp2.0 and net461. This application references a NuGet package:
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp2.0; net461</TargetFrameworks>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="runtime.native.Company.Project" Version="1.0.0" />
  </ItemGroup>
</Project>
The NuGet package contains a number of native dll files in the runtimes\win7-x64 directory. 
The application should be able to use P/Invoke to call the unmanaged code from the NuGet package:
using System.Runtime.InteropServices;
class Program
{
    static void Main(string[] args)
    {
        scsql_free_memory();
    }
    [DllImport("scsqlprocessor")]
    public static extern uint scsql_free_memory();
}
This works fine when using netcoreapp2.0: dotnet run -f netcoreapp2.0. With net461, it fails:
> dotnet run -f net461
Microsoft (R) Build Engine version 15.5.180.51428 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
  Restore completed in 15.89 ms for C:\Users\User\Desktop\reproduce\src\Reproduce.csproj.
Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'scsqlprocessor': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
   at Program.scsql_free_memory()
   at Program.Main(String[] args)
It seems like the unmanaged assemblies are not loaded with net461. I'm suspecting that this is because of how .NET Framework loads assemblies on demand. Does this mean that I have to manually load the unmanaged assemblies? If so, is there a way to do that in a way that works cross-platform? 
User contributions licensed under CC BY-SA 3.0