.NET7 or 8 - WPF - Accessing Windows.Storage throw System.TypeInitializationException

37 Views Asked by At

How do we access Windows.Storage with either .NET7 or 8 and WPF Core packaged for the store?

As an example, we used to do:

Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Information()
            .WriteTo.File(Path.Combine(ApplicationData.Current.LocalFolder.Path, "app.log")
            .CreateLogger();

However, ApplicationData.Current.LocalFolder.Path is now throwing:

System.TypeInitializationException: 
'The type initializer for 'WinRT.ActivationFactory`1' threw an exception.'

Inner Exception:
COMException: ClassFactory cannot supply requested class

Just for info:

<PropertyGroup>
  <OutputType>WinExe</OutputType>
  <TargetFramework>net7.0-windows10.0.22621.0</TargetFramework>
  <Nullable>enable</Nullable>
  <ImplicitUsings>enable</ImplicitUsings>
  <UseWPF>true</UseWPF>
</PropertyGroup>

It used to work under Windows 10, and can't seem to find any docs on the topic, is anyone aware?

1

There are 1 best solutions below

0
Franck E On BEST ANSWER

The solution that worked for me after fiddling around, was to install package Microsoft.Windows.Compatibility, together with the SDK.Buildtools.

enter image description here