wpf.net-corewindows-runtime.net-standard-2.0windows-application-packaging

Issue while using .NET Standard 2.0 library that uses Store API in WPF .NET Core 8.0 type of project


Hi fellow MSFT developers,

I have created a sample project for your convenience, available at

https://github.com/JiyaDesai-FandCo/WpfAppdotnet8

We have existing code in Library (of type .NET Standard 2.0) that heavily uses Store APIs, Addon and stuff. and we reuse this library in all our Store App projects. Currently this library is used in WPF (.NET 4.8) and UWP projects and all is running fine.

Problem occurred when we decided to create WPF project with .NET Core 8.0 That's where we are unable to use the library.

Now let me explain the sample project on github, which you can download, explore and help us to identify issue or workaround. The aim is to reuse library in all kind of projects. (WPF with .NET 4.8, UWP and WPF with .NET Core 8.0 ). Keeping in mind we are going to publish it on Store.

Also Note: while you debug, please always run packaging projects only.

ClassLibrarydotnetStandard2 :

  1. Has reference Microsoft.Windows.SDK.Contracts version 10.0.22621.2428 nuget package
  2. Has a function that calls Windows.ApplicationModel.Package.Current;
  3. public string LibraryFunctionToGetPackageName() { ... }
  4. Assume it has other functions related to Store API

xWpfApp1 : WPF with .NET Framework 4.8

  1. References ClassLibrarydotnetStandard2
  2. Calls function : var s = c.LibraryFunctionToGetPackageName();
  3. (Do not run xWpfApp1 directly, instead run xWpfApp1_Package.)

xWpfApp1_Package : Packaging project for xWpfApp1

  1. Compile and Run.
  2. Click on button #1 to call library function
  3. You should see, it does succeed.

WpfAppdotnet8 : WPF with .NET Core 8.0

  1. References ClassLibrarydotnetStandard2
  2. Calls function: this.Title = c.LibraryFunctionToGetPackageName();
  3. (Same, do not run WpfAppdotnet8 directly, instead run below packaging project.)

WPFdotnet8_PackageToPublishToStore : Packaging project for WpfAppdotnet8

  1. Compile and Run the package project
  2. Click on button #1
  3. You will see Error during library call. Exception: "System.PlatformNotSupportedException"

We want to resolve this error, so that we can use library successfully in WpfAppdotnet8

Hope I was able to explain.

Thanks & Regards

Awaiting your response very curiously.


Solution

  • .NET 6 and later use the target framework moniker option to consume Windows Runtime APIs:

    Call Windows Runtime APIs in desktop apps

    This means that you should build multiple versions of your class library:

    <PropertyGroup>
      <TargetFrameworks>netstandard2.0;net6.0-windows10.0.17763.0</TargetFrameworks>
    </PropertyGroup>
    
    <ItemGroup>
      <PackageReference Condition="'$(TargetFramework)' == 'netstandard2.0'"
                        Include="Microsoft.Windows.SDK.Contracts" Version="10.0.22621.3233">
        <PrivateAssets>All</PrivateAssets>
      </PackageReference>
    </ItemGroup>
    

    You should also include the OS version in the TFM in the .NET 8 project:

    <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>