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

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

  1. I have developed a library (net standard) and I used dependencies from net standard 1.6. Is there a was to use it from UWP or do I must convert it to a PCL? What are my options?

    Project MyProject is not compatible with uap10.0 (UAP,Version=v10.0) / win10-x86-aot. Project MyProject supports: netstandard1.6 (.NETStandard,Version=v1.6)

    Thx!

    Like

  2. I am new to the Visual Studio IDE. I have successfully installed the Visual Studio 2015, Update 3 environment for UWP.

    If I choose a Blank Template in UWP to develop a new Project in Visual Basic for a Desktop, the Object Browser only shows a subset of Visual Basic. I overcame this problem, by using the Classic Desktop Template and wrote a simple Application (Read/Write from Files) successfully. Maybe, by using the Classic Desktop Template, I will find, in future, that I have defeated the purpose of developing in UWP, once I go into the more complex stuff (such as cross platform Apps and communications between platforms?)

    The same happens when choosing a Template for developing a Project in Visual Basic for Windows Phone. In this case, it seems that there are no other Templates available, which I can use to overcome the problem.

    Hence, I failed to set-up a UWP Development Environment to perform full development in Visual basic, especially for Windows Phone?

    How do I incorporate the full Visual Basic Component in Desktop Development and Windows Phone Development and still have the power of UWP?
    Will appreciate assistance.

    Like

    1. Hi Steve,

      with UWP you only have a subset of the .NET Framework and not the full framework. What do you need from the full framework? With UWP you also have the Windows Runtime available, e.g. to access files. Files and Streams in my book Professional C# 6 shows you how you can access the file system using the full framework, and how to do it with the Windows Runtime. You can also read how to convert streams from .NET to the Windows Runtime and the other way around.

      With UWP you can use all Windows 10 platforms, e.g. the phone in addition to the desktop. With WPF, your application only runs on the desktop.

      Cheers,
      Christian

      Like

      1. Hi Cristian

        Thank you for the response.

        Yes, I have subsequently read about the .Net Core Framework that has been introduced as a new “lighter” .Net framework for better development across different platforms and various other advantages.

        I have already developed a simple Visual Basic application using StreamReader and StreamWriter in UWP, so no problem in this area.

        I, however, need to do string manipulation in Visual Basic like, viz. using Mid, Instr, InstrRev, Etc. (or similar functions?). Is there a work-around this in UWP?

        Thank you for the advice thus far and you patience.

        Regards
        Steve

        Like

  3. Hi Steve,

    I see – you are missing some VB specific functionality.
    I’m not a VB specialist, but I think I can help you here: you can use functionality available with the String class, e.g. s.Substring(), Integer.TryParse() to convert a string to an int, and the string format $”a hex number: {number:X}” to use formats for numbers, dates, and also custom formats.

    Cheers,
    Christian

    Like

  4. Hi Christian

    Thank you for your response.

    It seems like there is no easy solution for String Manipulation in Visual Basic for UWP.

    Apart from Assignments, Looping and Decision Structures, String Manipulation should be part of the basics of any programme language. It looks like the String Manipulation was omitted from UWP, restricting Visual Basic, C#, C++ and Java Script programmers from enjoying the full experience of programming.

    My thoughts went into anther direction, by converting a string to an array and to perform all String Manipulations in such an indirect way, but when I went through the UWP Class, looking for methods to do this, every time I came to a dead-end. I had a look at the following classes, inter alia:
    – Array
    – String – Using s.SubString – not included in UWP
    – Buffer
    – Text

    You can make it work if you can convert a String to a Byte Array, but non of the UWP classes permits you to do that. Viz. if you could use a Decoder, from the System.Text Class, to change a String into an array, there are lots of methods to manipulate the array. UWP, however, restricts all Classes and/or Methods to convert a String to an array.

    The next probable solution would be to investigate, how to create Windows Runtime Components in Visual Basic and see if I can Import such a component in my Development Environment, but Ghee!!! what a frustration. This will imply that I will have to create a Runtime component for each function.

    Again, thank you for your patience, but if the .Net Core Framework is omitting such trivial programming constructs, I am concerned that the programming world has lost focus on reality and got lost in the maize of layer-upon-layer interfacing which resulted in a monster of bureaucracy. Apologies, I come from the era of Assembler Language, where programmers was in full control of what they were programming.

    I do not want to take up more of your time. Maybe you can highlight this problem to the programming community, or bring it under the attention of the Guru’s behind the development of .Net Core Framework. For me, what a frustration. I want to stay abreast of the latest technology as a Computer Scientist, but as mentioned – a very big concern.

    Regards
    Steve

    Like

    1. Hi Steve,

      of course Substring is included with UWP as well. As you’ve written this now I had to try it with VB as well. This code compiles with UWP/VB:

         Private Async Sub OnButtonClick(sender As Object, e As RoutedEventArgs)
      Dim s As String = “the quick brown fox”
      Dim quick As String = s.Substring(4, 5)
      Dim dlg = New MessageDialog(quick)
      Await dlg.ShowAsync()
      End Sub

      Is this not working for you? What are you trying to do?

      Greetings,
      Christian

      Like

  5. Hi Christian

    Thank you for the effort and again your patience. The Substring does work and is exactly what I was looking for.

    Where I went wrong is to type in System.String.”Any Character” to see which methods are available in UWP in Visual Studio. If you do that, you will not find SubString listed as a method, because the String object has not been referenced and instantiated at this point in time and the methods will not be visible. I wrongly assumed that Visual Studio will display all methods available in the way I did it.

    I was really worried for a while.

    It may well be, that I will find lots of other methods that do work.

    Thank you again, I can now continue and again enjoy my programming.

    Regards
    Steve

    Like

    1. Hi Steve,
       
      ok, you only see static (shared with VB) members in that case, and Substring is an instance method.
      It’s great that you can continue now. Enjoy programming, and

      A Merry Christmas!

      All the best,
      Christian

      Like

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