Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use await in Portable Class Library for Win 8 and Win Phone 8

I'm attempting to create a Portable Class Library in Visual Studio 2012 to be used for a Windows 8 Store app and a Windows Phone 8 app.

I'm getting the following error:

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

At this line of code:

StorageFolder guidesInstallFolder = await Package.Current.InstalledLocation.GetFolderAsync(guidesFolder);

My Portable Class Library is targeted at .NET Framework 4.5, Windows Phone 8 and .NET for Windows Store apps.

I don't get this error for this line of code in a pure Windows Phone 8 project, and I don't get it in a Windows Store app either so I don't understand why it won't work in my PCL.

The GetAwaiter is an extension method in the class WindowsRuntimeSystemExtensions which is in System.Runtime.WindowsRuntime.dll. Using the Object Browser I can see this dll is available in the .NET for Windows Store apps component set and in the Windows Phone 8 component set but not in the .NET Portable Subset. I just don't understand why it wouldn't be in the Portable Subset if it's available in both my targeted platforms.

like image 608
Harry Len Avatar asked Dec 15 '12 22:12

Harry Len


2 Answers

You need the Async targetting pack on NuGet here for async/await to work for that combination of targets.

UPDATE:

Try this (nonsense) code snippet to check if it is using async/await correctly.

public async void MyMethodAsync()
{
    var req = WebRequest.Create("");
    await req.GetRequestStreamAsync();
}

However even if you get past the first problem of async/await not being available, the Package API you are calling is not available in the PCL.

like image 157
Paul Annetts Avatar answered Nov 11 '22 03:11

Paul Annetts


I just don't understand why it wouldn't be in the Portable Subset if it's available in both my targeted platforms.

The portable subset is not just everything that's common. Every member of the PCL is there deliberately, and there are a good number of members not included.

If a profile is missing something you need, request Microsoft to add it (via MSConnect or on the Q&A tab of the old-but-still-monitored PCL page).

like image 41
Stephen Cleary Avatar answered Nov 11 '22 04:11

Stephen Cleary