Anycpu nuget package requires either a 32-bit or 64-bit package

I have (anycpu) the nuget package "my_shared_library", which should reference the nuget package "non_anycpu_dependency", which comes in either 32-bit or 64-bit (this is the database access DLL).

I want 32-bit and 64-bit applications to be able to use "my_shared_library".

How can I use both 32-bit and 64-bit "my_shared_library" programs?

I thought I would have to either have 2 versions of "my_shared_library", or maybe there is some way at runtime to select the correct 32-bit / 64-bit nuget package "non_anycpi_dependency" based on the bit-time of the execution.

Has anyone solved this problem? Since most database dlls are not anycpu, I think this is a common problem.

Thanks in advance,

+3
32bit-64bit nuget nuget-package
source share
2 answers

Has anyone solved this problem?

Yes, Microsoft. They had to solve this same problem for their SQL Server Compact package. It has a single managed assembly, System.Data.SqlServerCe.dll, acting as an adapter for the managed program and a bunch of native DLLs that implement the real database engine. The way they bind to local DLL entry points is not what I ever recommend, I will work on the assumption that you are using pinvoke. I recommend using this method so that the client program can start AnyCPU.

Echoing the gist of this post, you want to execute this code somewhere in an initialization method or static constructor:

public static void SetupDatabaseBinaries() { var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); path = Path.Combine(path, IntPtr.Size == 8 ? "amd64" : "x86"); bool ok = SetDllDirectory(path); if (!ok) throw new System.ComponentModel.Win32Exception(); } [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool SetDllDirectory(string path); 

The only non-trivial step is to force the user project to copy its own DLLs into the amd64 / x86 project directories. This requires a post-assembly step, as the Compact package does. You will want to download the Nuget package to see how they did it, this is not very trivial. Launch VS, create a dummy project in console mode and get the Nuget package for Sql Server Compact. After installing it, use the tab "Projects + Properties", "Build Events" and note how she added this event after assembly:

 if not exist "$(TargetDir)x86" md "$(TargetDir)x86" xcopy /s /y "$(SolutionDir)packages\Microsoft.SqlServer.Compact.4.0.8876.1\NativeBinaries\x86\*.*" "$(TargetDir)x86" if not exist "$(TargetDir)amd64" md "$(TargetDir)amd64" xcopy /s /y "$(SolutionDir)packages\Microsoft.SqlServer.Compact.4.0.8876.1\NativeBinaries\amd64\*.*" "$(TargetDir)amd64" 

Nothing special, it does nothing but create the x86 and amd64 subdirectories and copies the DLLs into them. The tricky part is that your Nuget package adds this build event. Look at the package folder for the magic that did it. You probably want to follow your example as close as possible to avoid mistakes. Look at the \ Microsoft.SqlServer.Compact.4.0.8876.1 \ tools directory, it contains two PowerShell script files that do this. Install.ps1 starts at the end of the installation. The function that creates the post-build step is in VS.psm1, Add-PostBuildEvent. Good luck to you.

+8
source share

As soon as you refer to a package designed for a specific bit, you get stuck. You may be able to get a sufficient indirect relation to compile, but in the end you will end up with a BadImageFormatException .

What you can do is create an AnyCPU assembly that contains interfaces that will correspond to the specific implementations found in the specific assemblies that you have. Then all your AnyCPU assemblies should only reference interfaces.

In the exe that is going to control the application, you have to choose whether it will be 64 or 32 bits. In this EXE assembly, you reference the original 32 or 64-bit assembly (or the nuget package), if necessary.

To use this solution, a dependency injection infrastructure such as Autofac, StructureMap, or Unity will help reduce the pain of management by providing concrete implementations for your classes.

+1
source share

All Articles