How to fix Skiasharp native reference issue in Blazor NET90 app?

18 Nov 20241 minute to read

If you encounter loading issues or errors when using SkiaSharp in Blazor WebAssembly or WebApp projects, particularly in setups involving Emscripten 3.1.56, a workaround involves modifying your project file. This issue arises from specific native linking configurations, which may cause conflicts or improper behavior during runtime.

To address these issues, add the following code to your .csproj file:

<Target Name="RuntimeIssue109289_Workaround" AfterTargets="_BrowserWasmWriteRspForLinking"> 
    <ItemGroup> 
        <_WasmLinkStepArgs Remove="@(_EmccLinkStepArgs)" /> 
        <_EmccLinkStepArgs Remove="&quot;%(_WasmNativeFileForLinking.Identity)&quot;" /> 
        <_WasmLinkDependencies Remove="@(_WasmNativeFileForLinking)" /> 
        <_SkiaSharpToReorder Include="@(_WasmNativeFileForLinking)" Condition="$([System.String]::Copy('%(FullPath)').Contains('libSkiaSharp.a'))" /> 
        <_WasmNativeFileForLinking Remove="@(_SkiaSharpToReorder)" /> 
        <_WasmNativeFileForLinking Include="@(_SkiaSharpToReorder)" /> 
        <_EmccLinkStepArgs Include="&quot;%(_WasmNativeFileForLinking.Identity)&quot;" /> 
        <_WasmLinkDependencies Include="@(_WasmNativeFileForLinking)" /> 
        <_WasmLinkStepArgs Include="@(_EmccLinkStepArgs)" /> 
    </ItemGroup> 
</Target>
  • Purpose : This code block restructures the linking process to mitigate issues arising from SkiaSharp’s native library (libSkiaSharp.a) inclusion.

  • Conditions : The workaround is conditioned to only affect files related to SkiaSharp, preserving other components integrity.

Additional Resources

For further insights and updates on this issue, please refer to the following resources: