I'm trying to load the .NET Standard 2.0 nuget package onto a .NET Framework 4.7.2 Console Application but the .NET Framework 4.7.2 Console Application can't find the native dlls that are part of the nuget package. I used to have this issue with .NET Core 3.1 Console Application but I resolved it by making a targets file below (second code snippet).
Nuget package structure:
root
+-- build
+-- A.targets
+-- lib
+-- netstandard2.0
+-- A.dll
+-- A.xml
+-- runtimes
+-- win-x64
+-- native
+-- avcodec-58.dll
+-- ... (more native dlls)
A.targets content:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Condition=" '$(Platform)' == 'x64' OR '$(Platform)' == 'AnyCPU' ">
<Content Include="$(MSBuildThisFileDirectory)..\runtimes\win-x64\native\avcodec-58.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>avcodec-58.dll</Link>
</Content>
</ItemGroup>
<!-- (more native dlls) with same format above-->
</Project>
Problem: When I build the .NET Framework 4.7.2 Console Application I get this warning
Resolved file has a bad image, no metadata, or is otherwise inaccessible. System.BadImageFormatException: Could not load file or assembly 'avformat-58.dll' or one of its dependencies. The module was expected to contain an assembly manifest.
When I try to run the code in same Console App by referencing some code from the nuget package, I get a System.DllNotFoundException for one of the native dlls.
System.DllNotFoundException
HResult=0x80131524
Message=Unable to load DLL 'avformat.58': The specified module could not be found.
Has anybody encountered a similar issue? Thanks
Update (12/14/21) I was able to get rid of the warning System.BadImageFormatException build time warning when I also placed the native files in build\netstandard2.0\win-x64. However, during runtime the System.DllNotFoundException for the native dlls. The nuget package structure looks like this now:
root
+-- build
+-- A.targets
+-- netstandard2.0
+-- win-x64 (new folder and files)
+-- avcodec-58.dll
+-- ... (more native dlls)
+-- lib
+-- netstandard2.0
+-- A.dll
+-- A.xml
+-- runtimes
+-- win-x64
+-- native
+-- avcodec-58.dll
+-- ... (more native dlls)
A.targets now: I changed from Content -> None based on this post and added another None element for native files under build\netstandard2.0\win-x64.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Condition=" '$(Platform)' == 'x64' OR '$(Platform)' == 'AnyCPU' ">
<None Include="$(MSBuildThisFileDirectory)..\runtimes\win-x64\native\avcodec-58.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>avcodec-58.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)\netstandard2.0\win-x64\avcodec-58.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>avcodec-58.dll</Link>
</None>
</ItemGroup>
<!-- (more native dlls) with same format above-->
</Project>