question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SKDocument.CreateXps() returns null in a .NET Core App

See original GitHub issue

Hi,

Creating an Xps Document fails when run in a dotnet core app (the returned document instance is null), while the same code works with the full .NET Framework.

using (var stream = new SKFileWStream("test.xps"))
using (var document = SKDocument.CreateXps(stream))
{
 // document = null if run from a dotnet core 2.0 app!
}

I am using the newest SkiaSharp nuget: 1.60.0

(FYI: SKDocument.CreatePdf() works fine from a dotnet core app.) As the Xps Backend is provided by Skia I would expect it to work in SkiaSharp as well.

Take care, Martin

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mattleibowcommented, Mar 23, 2018

I managed to find the issue. It was the fact that the COM was not initialized.

https://msdn.microsoft.com/en-us/library/windows/desktop/ff485844.aspx

With the full .NET Framework, COM is already initialized as it is used by the framework itself. I am not sure how I can integrate this into SkiaSharp as this must be done outside the scope of SKDocument. the native skia has a nice utility type that I think I may be able to duplicate…

Here is a workaround:

class Program
{
	static void Main(string[] args)
	{
		CoInitializeEx(IntPtr.Zero, COINIT.COINIT_APARTMENTTHREADED | COINIT.COINIT_DISABLE_OLE1DDE);

		using (var stream = new SKFileWStream("test.xps"))
		using (var document = SKDocument.CreateXps(stream))
		{
			Console.WriteLine($"document == '{document}'");

			document.BeginPage(100, 100);
			document.EndPage();
			document.Close();
		}

		Console.WriteLine("Hello World!");

		CoUninitialize();
	}

	public enum COINIT : uint
	{
		COINIT_MULTITHREADED = 0x0,
		COINIT_APARTMENTTHREADED = 0x2,
		COINIT_DISABLE_OLE1DDE = 0x4,
		COINIT_SPEED_OVER_MEMORY = 0x8,
	}

	[DllImport("ole32.dll", CharSet = CharSet.Auto, SetLastError = true, CallingConvention = CallingConvention.StdCall)]
	public static extern int CoInitializeEx([In, Optional] IntPtr pvReserved, [In]  COINIT dwCoInit);

	[DllImport("ole32.dll", CharSet = CharSet.Auto, SetLastError = true, CallingConvention = CallingConvention.StdCall)]
	public static extern void CoUninitialize();
}
0reactions
mattleibowcommented, Mar 23, 2018

The fix in v1.60.1 will be:

using (new SKAutoCoInitialize()) {
   // use XPS bits
}

NOTE: this is only needed for some .NET Core apps. The full .NET Framework already initializes COM.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SKCanvas Class (SkiaSharp)
CreateXps (stream); // get the canvas from the page var canvas = document. ... The null canvas is a canvas that ignores all...
Read more >
Why C# PowerShell.Create() returns null in case of .NET ...
Create() returns the proper PowerShell object; When I call same code from the .NET Core project then it returns null .
Read more >
How to Troubleshoot Asp.net Core ...
CloudConfigurationManager.GetSetting() returning null values can be caused by several reasons, including incorrect settings, unavailable ...
Read more >
ASP.NET Core FromBody: Not working? Or returning null ...
The ASP. NET Core FromBody. Is it not working? Or returning null ? This video will provide a solution to this common .NET...
Read more >
How do you handle EF Core null return values in your ...
The Id is sent from the client application and if they have access to an Id that doesn't exist in the database, then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found