Skip to content

How to succeed in using NuGet packages with native binaries (like e.g. SkiaSharp) #397

@UweKeim

Description

@UweKeim

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:

C:\P\MYPROJECT\SOURCE\WEBAPP\BIN\RELEASE\NET8.0\RUNTIMES
├───osx
│   └───native
│           libSkiaSharp.dylib
│
├───win-arm64
│   └───native
│           libSkiaSharp.dll
│
├───win-x64
│   └───native
│           libSkiaSharp.dll
│
└───win-x86
    └───native
            libSkiaSharp.dll

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"::

//css_nuget SkiaSharp
//css_nuget SkiaSharp.NativeAssets.Win32

using SkiaSharp;

class Program
{
    static void Main()
    {
        try
        {
            int width = 800;
            int height = 600;

            using var bitmap = new SKBitmap(width, height);
        }
        catch (Exception x)
        {
            Console.WriteLine(x.ToString());
            while(x.InnerException!=null)
            {
                x = x.InnerException;
                Console.WriteLine();
                Console.WriteLine(x.ToString());
            }
        }
    }
}

Running it from my Windows 11 command line with this:

css /dbg "skiasharp-test.cs"

This results in the following:

System.TypeInitializationException: The type initializer for 'SkiaSharp.SKImageInfo' threw an exception.
 ---> System.DllNotFoundException: Unable to load DLL 'libSkiaSharp' or one of its dependencies: Das angegebene Modul wurde nicht gefunden. (0x8007007E)
   at SkiaSharp.SkiaApi.sk_colortype_get_default_8888()
   at SkiaSharp.SkiaApi.sk_colortype_get_default_8888()
   at SkiaSharp.SKImageInfo..cctor() in /_/binding/SkiaSharp/SKImageInfo.cs:line 48
   --- End of inner exception stack trace ---
   at SkiaSharp.SKBitmap..ctor(Int32 width, Int32 height, Boolean isOpaque) in /_/binding/SkiaSharp/SKBitmap.cs:line 33
   at Program.Main() in C:\Ablage\skiasharp-test.cs:line 15

System.DllNotFoundException: Unable to load DLL 'libSkiaSharp' or one of its dependencies: Das angegebene Modul wurde nicht gefunden. (0x8007007E)
   at SkiaSharp.SkiaApi.sk_colortype_get_default_8888()
   at SkiaSharp.SkiaApi.sk_colortype_get_default_8888()
   at SkiaSharp.SKImageInfo..cctor() in /_/binding/SkiaSharp/SKImageInfo.cs:line 48

The exception reads (in German):

System.DllNotFoundException: Unable to load DLL 'libSkiaSharp' or one of its dependencies: Das angegebene Modul wurde nicht gefunden. (0x8007007E)

And in English:

System.DllNotFoundException: Unable to load DLL 'libSkiaSharp' or one of its dependencies: The specified module could not be found. (0x8007007E)


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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions