Problems using .NET Core with UWP?

Probably you’ve issues using .NET Core libraries from Universal Windows Platform apps. If you get errors such as package restore failed, and issues such as detected package downgrade or not compatible with netstandard this article can give you answers.

Toolbox

Package Restore Failed

Creating a Universal Windows Platform app using Visual Studio 2015 Update 3, and adding a .NET Core package such as Microsoft.Extensions.DependencyInjection, you might get this error on adding the package:

Package restore failed. Rolling back changed.

Looking at the errors in the Output window, you might find many package downgrades, e.g.

Detected package downgrade: System.Runtime.WindowsRuntime from 4.0.11 to 4.0.10

Detected package downgrade: Microsoft.Win32.Primitives from 4.0.1 to 4.0.0

Detected package downgrade: System.Runtime.WindowsRuntime

The reason for this problem is that the Universal Windows Platform was the first client of parts of .NET Core. Since the release of UWP, a lot has happended with .NET Core. This error can be solved easily by updating the package Microsoft.NETCore.UniversalWindowsPlatform to the version 5.2.2. You can do this either using the NuGet Package Manager, or directly change the version number within the file project.json. After saving the file, the new package is downloaded.

Update NuGet Package

This update results in a huge list of package updates and will solve many problems using .NET Core packages.

UWP Package Updates

After the package update is complete, many .NET Core packages can be added without problems, e.g. Microsoft.Extensions.DependencyInjection.

The new project.json including a reference to Microsoft.Extensions.DependencyInjection looks like this:

{
  "dependencies": {
    "Microsoft.Extensions.DependencyInjection": "1.0.0",
    "Microsoft.NETCore.UniversalWindowsPlatform": "5.2.2"
  },
  "frameworks": {
    "uap10.0": {}
  },
  "runtimes": {
    "win10-arm": {},
    "win10-arm-aot": {},
    "win10-x86&amp": {},
    "win10-x86-aot": {},
    "win10-x64": {},
    "win10-x64-aot": {}
  }
}

Creating Custom libraries

Custom libraries that can be used across different platforms (e.g. using WPF/UWP/Xamarin), can be created as a portable class library. For the library used here I’m selecting .NET Framework 4.6, Windows Universal 10.0, Xamarin.Android, and Xamarin.iOS as targets.

Portable Class Library

After the project was created, you can target the .NET Platform Standard from the project properties.

Targeting .NET Platform Standard

Based on the selection of the targets, .NET Standard 1.2 is chosen. The resulting project.json looks like this:

{
  "supports": {},
  "dependencies": {
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
    "NETStandard.Library": "1.6.0"
  },
  "frameworks": {
    "netstandard1.2": {}
  }
}

The NuGet package Microsoft.Extensions.DependencyInjection can be added to this project.

Not Compatibility with .NETStandard1.2

However, with some packages it does not work adding them to the .NET Standard library, e.g. trying to add Prism.Core results in the error Package restore failed. Rolling back package changes…. The Output window gives more information about the issue:

Package Prism.Core 6.1.0 is not compatible with netstandard1.2.

Package Prism.Core 6.1.0 supports:

  • monoandroid10 (MonoAndroid, Version=v1.0)
  • monotouch10 (MonoTouch, Version=v1.0)
  • net45 (.NETFramework, Version=v4.5)
  • portable-monoandroid10+monotouch10+net45+win+win81+wp8+wp81+xamarinios10 (.NETPortable,Verson=v0.0)
  • wp8 (WindowsPhone,Version=v8.0)
  • wp81 (WindowsPhone,Version=v8.1)
  • wpa81 (WindowsPhoneApp,Version=v8.1)
  • xamarinios10 (Xamarin.iOS,Version=v1.0)

This NuGet package supports several platform. However, the Target Framework Moniker (TFM) netstandard is missing from this list. For now, this can be easily resolved by adding imports to the frameworks section as shown:

"frameworks": {
  "netstandard1.2": {
    "imports": "portable-monoandroid10+monotouch10+net45+win+win81+wp8+wpa81+xamarinios10"
  }
}

The imports keyword is a temporary workaround. Imports references TFMs that should be treated as compatible to netstandard1.2. With the imports section, you can either specify one TFM, or an array of multiple TFMs. Defining multiple TFMs is useful in case you use multiple packages that support different TFMs but still would work with the framework.

As time goes on, it should not be necessary to add imports to project.json. This is just a workaround currently needed to allow using NuGet packages that don’t specify NetStandard with the package definition.

With the imports specification in project.json, the package Prism.Core can be added to the project.

Portable libraries nowadays are defined with a complex matrix and every version of one TFM added increases the complexity in what types and members are available. With NetStandard, the definition of types and members just goes in one direction. With every version added, more types and members are available.

Summary

Over time .NET Core and NETStandard will make programming easier. This is probably not yet the case, as some libraries can not be added to a project without making a few small changes. However, as you know what needs to be done – updating Microsoft.NETCore.UniversalWindowsPlatform, and specify needed TFMs in the imports section is often all what’s needed to get rid of a lot of errors, and you can make use of cool new .NET Core features.

It would be great to hear from you – if this blog post solved your issues using UWP with .NET Core, and also if you have different issues.

Have fun with programming and learning!
Christian

More Information

More information on creating Universal Windows apps is available in my new book Professional C# 6 and .NET Core 1.0, and my workshops. Workshops can be customized to your specific needs.

Related Information

.NET Standard Library

Portable Library, you’ll not be missed

Using .NET Core Libraries from UWP

Toolkit image © Dave Bredeson | Dreamstime.com Toolbox Tools Box

3 thoughts on “Problems using .NET Core with UWP?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s