3

I've copied the same piece of code from one project to another and in the first one it compiles fine, but in the second one it doesn't.

The piece is:

using Windows.Devices.Bluetooth;
// a couple of lines of other code
var rfcommProvider = await RfcommServiceProvider.CreateAsync(RfcommServiceId.FromUuid(RfcommChatServiceUuid));

Error:

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

The first project is demo created by Microsoft and it is a Windows 8.1 application.

My application is a normal WPF application and it is targeting .NET 4.5.1 and Windows 8.1 and I added the reference to the Windows.winmd as described here

I do not know why the same piece of code might compile in one application and not compile in the other one when all common things like references, namespaces, .. are OK. My only idea is that maybe I'm referencing some older version that is not yet awaitable, but I have no idea how to verify this.

i3arnon
  • 113,022
  • 33
  • 324
  • 344
kubal5003
  • 7,186
  • 8
  • 52
  • 90
  • possible duplicate of [StorageFile Async usage in a NON Metro application](http://stackoverflow.com/questions/10428446/storagefile-async-usage-in-a-non-metro-application) – Yuval Itzchakov Jan 03 '15 at 20:29

1 Answers1

7

The error is that IAsyncOperation doesn't have a GetAwaiter method. It exists in System.Runtime.WindowsRuntime.dll which you need to add as a reference since you are using WinRT code in a .Net environment:

System.Runtime.WindowsRuntime.dll

More in How to use specific WinRT API from Desktop apps.
Reference 1 Reference 2

Osama Rizwan
  • 615
  • 1
  • 7
  • 19
i3arnon
  • 113,022
  • 33
  • 324
  • 344