SkiaSharp
SkiaSharp copied to clipboard
[BUG] System.TypeInitializationException : The type initializer for 'SkiaSharp.SKImageInfo' threw an exception in AWS Lambda function NET Core 3.1
Description
System.TypeInitializationException : The type initializer for 'SkiaSharp.SKImageInfo' threw an exception.
Code
Create an empty AWS Lambda Project (.NET Core - C#) in Visual Studio Refer: https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/lambda-creating-project-in-visual-studio.html
Add the below code in FunctionHandler
`
SKBitmap bitmap = new SKBitmap(100, 100, SKImageInfo.PlatformColorType, SKAlphaType.Premul);
var surface = SKSurface.Create(bitmap.Info);
SKCanvas canvas = surface.Canvas;
canvas.Clear(SKColors.White);
var paint = new SKPaint();
paint.IsAntialias = true;
paint.Color = SKColors.Red;
paint.StrokeWidth = 3;
canvas.DrawCircle(50, 50, 25, paint);
var image = SKImage.FromBitmap(bitmap);
var data = surface.Snapshot().Encode(SKEncodedImageFormat.Jpeg, 80);
MemoryStream outputStream = new MemoryStream();
data.SaveTo(outputStream);
outputStream.Position = 0;
return "SkiaSharpImageCreation";
`
Install SkiaSharp v2.80.2, SkiaSharp.NativeAssests.Linux v2.80.2 Publish the application in AWS Lambda (Right click -> Publish AWS). Using simple ASP.NET Core application access this function, you can receive exception.
Expected Behavior
Should run without an exception
Actual Behavior
{ "errorType": "TypeInitializationException", "errorMessage": "The type initializer for 'SkiaSharp.SKImageInfo' threw an exception.", "stackTrace": [ "at SimpleAWS.Function.FunctionHandler(String input, ILambdaContext context) in D:\SimpleAWS\Function.cs:line 31", "at lambda_method(Closure , Stream , Stream , LambdaContextInternal )" ], "cause": { "errorType": "DllNotFoundException", "errorMessage": "Unable to load shared library 'libSkiaSharp' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibSkiaSharp: cannot open shared object file: No such file or directory", "stackTrace": [ "at SkiaSharp.SkiaApi.sk_colortype_get_default_8888()", "at SkiaSharp.SKImageInfo..cctor()" ] } }
Basic Information
-
Version with issue: SkiaSharp v2.80.2, SkiaSharp.NativeAssests.Linux v2.80.2
-
IDE: Microsoft Visual Studio Professional 2019, Version 16.8.4
-
Platform Target Frameworks: ASP.NET Core 3.1
Screenshot of code snippet:
Additional Information This issue not occur in ASP.NET Core 2.1 AWS Lambda application.
Sample to reproduce the issue: IssueReproduceSample.zip
Replication steps:
- Download and unzip.
- Publish SimpleAWS Sample.
- Run the ASP.NET Core sample by adding access key and secret key, to get response from AWS Lambda Function.
Please let me know if you need further details on this
Any quick solutions or workaround would be really helpful?
@MohanaselvamJ I am facing same issue, Have you got any solution on this
I ran into the same issue when I deployed some rendering logic that used to be done in a Xamarin app to AWS lambda. It turns out AWS lambdas use AWS Linux and to solve the issue, we need to install the NuGet package SkiaSharp.NativeAssets.Linux
. If you happen to have this issue on MacOS, same thing but with SkiaSharp.NativeAssets.macOS
.