Using async/await operators in a WP8 plugin

Hi there,

The question is, how can I use Async methods in a Windows plugin?

Well this afternoon for some hours I spent my time making an IAP plugin for WP8. I spent at least an hour searching for how to use wrap async/await operators in a .Net 3.5 framework class library, and finally found out encapsulation is the answer!

You simple need to mark the methods that use Async/await as private.

In your WP8 dll you’ll have some method like this that uses async:

    private static async void GetProductInfo(string productInfo)
    {
        var listing = await CurrentApp.LoadListingInformationAsync();

        //Blah blah code..
    }

Now if you’re reading this I assume you know that to use the plugin you need to make a ‘fake’ class library on-top of this so that the editor doesn’t pick up on the WP8 specific code.

All that needs to be done is to create a new class library project targeting .Net 3.5, and have it include every method you wish to use from the ‘real’ dll targeting the WP8 specific code, the method signatures must match, along with the assembly namespaces must match also.

This is what tripped me up, you can’t use Async in .Net 3.5.

Writing the following in the ‘fake’ class lib that targets .Net 3.5:

    private async void GetProductInfo(string productInfo)
    {
         //This method really can just be left empty as it won't be called
           from the Windows device
    }

Throws an error, and rightly so - Async is not implemented in .Net 3.5.

What you must do is:

In the ‘real’ dll you need to mark the async methods as private and have public methods call them, like so:

    private async void GetProductInfo(string productInfo)
    {
        var listing = await CurrentApp.LoadListingInformationAsync();

        //Blah blah code..
    }

    public void GetProductInfoPublic(string ProductInfoPublic)
    {
        //Call the private async method in the non-async public method
        GetProductInfo(ProductInfoPublic);
    }

And that’s it for the ‘real’ dll.

Now if you remember from above, the ‘fake’ dll must have matching method signatures for the methods you wish to use in your unity script.
Since we made the Async methods private, you don’t need to use them!

So in the ‘fake’ dll all you have to add is the following:

 public void GetProductInfoPublic(string ProductInfoPublic)
 {
       //Again nothing really needs to be here..
 }

And that’s you done! Calling the GetProductInfoPublic method above on a windows device will result in the method in the ‘real’ dll being called, which calls the private async method, calling it from the editor, or a non windows device will result in the method in the ‘fake’ dll being called.

One last thing, in both dlls’ both the ‘Default namespace’ and ‘Assembly name’ properties need to match.

In visual studio you can edit this by right-clicking on the project in the solution explorer, click properties, the two text boxes with the above two fields will be the first thing you see.

Hope that helps someone out there! Feel free to comment with any questions.