Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load UWP library into .NET Framework app

There are a number of articles(codeproject, blog1, blog2, forum) to use WinRT library into .Net Framework console application in Windows 8.

I tried it with UWP in Windows 10. But failed to make it. I struggled to compile without error, but it occurred BadImageFormatException in runtime.

This is what I did.

  1. Create Console application with .NET Framework 4.6.1 target.
  2. Edit .csproj file to add <TargetPlatformVersion>10.0</TargetPlatformVersion>
  3. Reference three libraries as below.
    • c:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd (shows Windows Runtime 1.4)
    • c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5.1\System.Runtime.WindowsRuntime.dll (shows 4.0.10.0)
    • c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5.1\System.Runtime.InteropServices.WindowsRuntime.dll (shows 4.0.0.0)

In contrary to Windows 8 examples, there is error when System.Runtime.dll is referenced.

The code is as below. Note that the code is from Microsoft forum.

class Program
{
    static void Main(string[] args)
    {

        Geolocator locator = new Geolocator();
        var status = Geolocator.RequestAccessAsync();

        if (status != null)
        {
            Console.WriteLine("not null");
            Task.Run(async () =>
            {
                Geoposition pos = await locator.GetGeopositionAsync();
                Console.WriteLine(pos.Coordinate.Accuracy);
            });
        }
        else
        {
            Console.WriteLine("null");
        }
        Console.ReadLine();
    }

Compilation is OK, and not null is displayed in console. So, invocation of library itself seems fine. But GetGeopositionAsync cause BadImageFormatException.

The exception message detail is as below.

Exception thrown: 'System.BadImageFormatException' in mscorlib.dll

Additional information: Could not load file or assembly 'System.Runtime.WindowsRuntime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)

I already tried (1) to change build configuration to x86/x64/AnyCPU (2) and to set Copy Local to true to all references, but same error occurred.

In my opinion, this exception is saying System.Runtime.WindowsRuntime tries to load some dependent library internally but I don't know what it is.

like image 894
Youngjae Avatar asked Dec 31 '15 13:12

Youngjae


People also ask

Does UWP use .NET framework?

NET Standard is supported on UWP.

How do I add UWP to Visual Studio?

On the Create a new project screen, enter Universal Windows in the search box, choose the C# template for Blank App (Universal Windows), and then choose Next. If you don't see the Blank App (Universal Windows) project template, click the Install more tools and features link. The Visual Studio Installer launches.

Does .NET core support UWP?

You can run UWP on . NET Core today.


2 Answers

Here are the steps working over here, note the subtle differences between yours:

Step 1: Add to .csproj file

  <PropertyGroup>
    <TargetPlatformVersion>10.0</TargetPlatformVersion>
  </PropertyGroup>

Step 2: Add reference to

C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd

Step 3: Add reference to

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll

Note the differences, v4.5 instead of v4.5.1, and no references to System.Runtime.InteropServices.WindowsRuntime.dll.

This works fine here (Win10, VS2015).

Side note, call to Geolocator will fail regardless:

This operation is only valid in the context of an app container. (Exception from HRESULT: 0x8007109A)

It won't be available from within desktop, only types referenced here are supported in desktop.

like image 68
aybe Avatar answered Oct 20 '22 07:10

aybe


I had this error and I upgraded the version of Microsoft.NETCore.UniversalWindowsPlatform from "5.0.0" to "5.2.2" and my program started working

like image 1
KyleUp Avatar answered Oct 20 '22 09:10

KyleUp