-
Notifications
You must be signed in to change notification settings - Fork 238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to succeed in using NuGet packages with native binaries (like e.g. SkiaSharp) #397
Comments
It's an interesting problem. Will have a look at it and include it in the very next release. |
Hm... this package does not rely on .NET interop locator (current dir + any dir from %PATH%). I tried |
OK, this one does: using SkiaSharp;
using static System.Reflection.BindingFlags;
var type = typeof(GRGlInterface);
var method = type.GetMethod("CreateGl", Static | NonPublic);
var gl = method.Invoke(null, new object[0]);
Console.WriteLine("Success"); |
OK their probing algorithm does respect %PATH% probing so it's doable. But at runtime the script engine needs to know which OS specific subdir to add to the %PATH%. |
Unfortunately I do not know😢. This is an area, I have literally nearly zero experiences in. Those ".targets" files are pure mystery to me. Honestly, I found a workaround to not require SkipaSharp for now, so it is not an issue for me for now. I just wanted to let you know that the NuGet support is limited in this specific area. Another package is e.g. "Microsoft.SqlServer.Compact" (totally outdated, but I still need to use it) for which I found a workaround by using some reflection: if(!_didSetBinariesPath)
{
_didSetBinariesPath = true;
var privateFolderPath = Afx.ReplacePlaceholders(
@"${ScriptFolderPath}\..\..\..\..\..\_References\Direct\SqlCe\4.0.8876.1\NativeBinaries");
var sqlCeAssembly = typeof(SqlCeConnection).Assembly;
var nativeMethodsType = sqlCeAssembly.GetType("System.Data.SqlServerCe.NativeMethods");
if (nativeMethodsType == null)
{
throw new Exception("[SQL-CE] Die Klasse 'NativeMethods' konnte nicht gefunden werden.");
}
var loadMethod = nativeMethodsType.GetMethod(
"LoadNativeBinariesFromPrivateFolder",
BindingFlags.NonPublic | BindingFlags.Static);
if (loadMethod == null)
{
throw new Exception("[SQL-CE] Die Methode 'LoadNativeBinariesFromPrivateFolder' konnte nicht gefunden werden.");
}
var result = (bool)loadMethod.Invoke(null, new object[] { privateFolderPath });
} |
OK, I got teh answer it is in fact strictly embedded in the package structure (part of the nuget spec) so it's fully possible to achieve. <package_root>/
├── lib/
│ └── <target_framework>/ # For managed assemblies
├── runtimes/
│ ├── win-x64/ # Native DLLs for Windows 64-bit
│ │ └── native/
│ │ └── your_native.dll
│ ├── win-x86/ # Native DLLs for Windows 32-bit
│ │ └── native/
│ │ └── your_native.dll
│ ├── linux-x64/ # Native DLLs for Linux 64-bit
│ │ └── native/
│ │ └── your_native.so
│ ├── osx-x64/ # Native DLLs for macOS 64-bit
│ │ └── native/
│ │ └── your_native.dylib
│ └── ...
├── build/ # MSBuild props and targets
├── tools/ # Tools for the developer
├── content/ # Additional content to include in projects
└── ... |
- #397: How to succeed in using NuGet packages with native binaries (like e.g. SkiaSharp) (Added support for nuget package native assets) - script compilation cache now stores probing dirs to allow recreation of PATH environemnt variable during the cached execution (e.g. to cover nuget native assets)
### CLI - #396 Some NuGet packages are not recognized and not referenced - #397: How to succeed in using NuGet packages with native binaries (like e.g. SkiaSharp) - Added new command `-list` for printing all currently running scripts. - Added support for nuget package native assets - LegacyNugetSupport by defauls made false - script compilation cache now stores probing dirs to allow recreation of PATH environemnt variable during the cached execution (e.g. to cover nuget native assets) - Added support for `-self-install` command to set global `CSSCRIPT_ROOT` envar. - Updated `//css_nuget` syntax CLI documentation ### CSScriptLib - no changes
Introduction
There are several NuGet packages that ship with a managed assembly and also with additional native binaries that are copied to the "runtimes" folder below the binaries.
SkiaSharp is an example of such a library:
There is a "SkiaSharp.dll" (managed) directly in "C:\P\MYPROJECT\SOURCE\WEBAPP\BIN\RELEASE\NET8.0" and the above folder structure.
In a C# application (e.g. ASP.NET Core MVC, Console or Windows Forms) the build system somehow ensures that the native binaries are copied to the output folder.
In CS-Script this is not the case. Resulting in runtime errors.
Example
As an example, consider the following minimal CS-Script file "skipasharp-test.cs"::
Running it from my Windows 11 command line with this:
css /dbg "skiasharp-test.cs"
This results in the following:
The exception reads (in German):
And in English:
I honestly do think that you thought about such cases and have a solution; I simply cannot figure it out for now.
My question
Is it possible that CS-Script supports NuGet packages both with managed and native binaries as e.g. with SkiaSharp?
More information
In case it helps, here is the SkiaSharp LibraryLoader.cs
I do think that this file is the reason why the binaries are automatically copied to the output folder. (See also the NuGet package "SkiaSharp.NativeAssets.Win32" in the "buildTransitive" folder).
The text was updated successfully, but these errors were encountered: