Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error awaiting DeviceInformation.FindAllAsync(selector) with System.Runtime.WindowsRuntime referenced

I'm writing code in a desktop application that discovers and connects to devices with Wi-Fi Direct for Windows 10. I've followed the instructions on how to call Windows Runtime API from non-WinRT environment, everything is fine on VS 2013 except the following lines and I move them in a console application targeting .NET Framework 4.5:

using System;

using Windows.Devices.Enumeration;
using Windows.Devices.WiFiDirect;

static void Main()
{
    DoSomethingAsync().Wait();
}

static async void DoSomethingAsync()
{
    var selector = WiFiDirectDevice.GetDeviceSelector(WiFiDirectDeviceSelectorType.DeviceInterface);
    foreach (var info in await DeviceInformation.FindAllAsync(selector)) // <-- error
    {
        ...
    }
}

And the error is still

'await' requires that the type 'Windows.Foundation.IAsyncOperation<Windows.Devices.Enumeration.DeviceInformationCollection>' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?

with following library references: image

  • System.Runtime, C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Runtime.dll
  • System.Runtime.WindowsRuntime, C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
  • Windows.Devices, C:\Windows\System32\WinMetadata\Windows.Devices.winmd
  • Windows.Foundation, C:\Windows\System32\WinMetadata\Windows.Foundation.winmd

I'm sure selector could be retrieved correctly (something like

System.Devices.InterfaceClassGuid:="{439B20AF-8955-405B-99F0-A62AF0C68D43}" 
AND System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True

), then I assume the hack is still valid for desktop applications on Windows 10.

How do I fix this error?

like image 679
Kidd Liu Avatar asked Jan 07 '23 08:01

Kidd Liu


2 Answers

Thank you very much, @Yuval Itzchakov, @vidalsasoon

It turns out I was mislead by the path where System.Runtime.WindowsRuntime.dll locates. In C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5 the library is actually of .NET Framework 4.0, in which no such extension methods(GetWaiter, AsTask...) are defined. However, it looks like the folder is the default location when you're referencing .NET Framework assemblies in VS.

Then I'm referencing

  • System.Runtime.WindowsRuntime.dll, of C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Runtime.WindowsRuntime.dll
  • Windows, of C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd

and now it's working.

like image 148
Kidd Liu Avatar answered Jan 09 '23 22:01

Kidd Liu


Try adding "AsTask()" to the IAsyncOperation and work around the issue that Yuval mentioned.

static void Main()
{
    var selector = WiFiDirectDevice.GetDeviceSelector();

    var findAllDevicesTask = DeviceInformation.FindAllAsync().AsTask();

    Task.WaitAll(findAllDevicesTask);

    for (var info in findAllDevicesTask.Result)
    {
        ...
    }
}
like image 34
vidalsasoon Avatar answered Jan 09 '23 22:01

vidalsasoon