CsWinRT: NullReferenceException and InvalidCastException when using Package

Seems like that some of the PackageManager functionality does not work correctly when using .NET 5 preview 8 and cswinrt- I tried running on all available net5.0-windows10.0.xxx versions (Preview 8), I had no issues with Microsoft.Windows.SDK.Contracts

The following code crashes only when running on Windows 1809 (10.0.17763) only:

var packages = new PackageManager().FindPackages();
Package logo = packages.First().Logo; // The logo getter throws NRE for ANY package

The exception:

Failed System.NullReferenceException: Object reference not set to an instance of an object.
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
   at ABI.Windows.ApplicationModel.IPackage2.get_Logo()

Another problem is (happens on every version)

var entries = await package.GetAppListEntriesAsync();
 // This throws InvalidCastException
var accessStreamReference = entries.First().DisplayInfo.GetLogo(new Size(256, 256));

The exception:

Failed entires System.InvalidCastException: Unable to cast object of type 'Windows.Storage.StorageFile' to type 'ABI.Windows.Storage.Streams.IRandomAccessStreamReference'.
   at Windows.Storage.Streams.RandomAccessStreamReference.FromAbi(IntPtr thisPtr)
   at ABI.Windows.Storage.Streams.RandomAccessStreamReference.FromAbi(IntPtr thisPtr)
   at ABI.Windows.ApplicationModel.IAppDisplayInfo.GetLogo(Size size)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (11 by maintainers)

Most upvoted comments

@j0shuams

Seems like RC2 fixes GetLogo for me (at least on 2004) Thanks!

@adirh3 Would you try this with .NET RC2 ? We believe that the updates we made and got moved into RC2 fix this error – @manodasanW stepped back to before the changes we added in RC2 and this repro’d and when he tried it with RC2 it ran fine.

And since you and Mano are able to use FindPackages* functions, I don’t think this is a cswinrt (or dotnet) issue. Maybe it is an issue with the packages I have installed or some kind of API bug in the OS.

Once package.GetLogo is fixed (the first problem), will it also not work in 1809? The change was made in 1903. It won’t appear in older releases unless it was serviced downlevel, and servicing tends to have a high bar i.e. most changes don’t get backported to older released versions of Windows. I’d guess this didn’t meet the bar.

One of Project Reunion’s goals is to help provide that sort of backported/downlevel functionality developers would like but Windows’ servicing can’t justify.

Package’s documentation was supposed to be updated to reflect this limitation lifted as of 1903. I’ll ping some folks to help get that addressed.