Mike Taulty's Blog
Bits and Bytes from Microsoft UK
Projecting Async .NET Code from a WinRT Component

Blogs

Mike Taulty's Blog

Elsewhere

Archives

This one arose today and it’s something that I’d thought about a little before and I’d had a good read of this blog post in the past about working with async at the WinRT level rather than at the .NET level.

Taking a simple example – let’s say that I want to write a function that downloads from microsoft.com, counts up the length of the stream returned and returns that to the caller.

In the async/await model of .NET, that seems fairly easy as I can lean on Task and await/async;

namespace App4
{
  using System;
  using System.IO;
  using System.Net.Http;
  using System.Threading.Tasks;

  static class MicrosoftDownloader
  {
    async public static Task<long> GetMicrosoftHomePageSize()
    {
      // local error handling omitted
      var length = 0L;
      HttpClient client = new HttpClient();

      HttpResponseMessage message = await client.GetAsync(
        new Uri("http://www.microsoft.com"));

      using (Stream stream = await message.Content.ReadAsStreamAsync())
      {
        length = stream.Length;
      }
      return (length);
    }
  }
}

and using that code might look something like;

 var x = await MicrosoftDownloader.GetMicrosoftHomePageSize();

which would need to be called from an async method to use that await keyword.

However, what happens if I take that .NET code as above and try and make it into a WinRT component to be used across C++, .NET and JavaScript Windows Store projects?

namespace WindowsRuntimeComponent1
{
  using System;
  using System.IO;
  using System.Net.Http;
  using System.Threading.Tasks;

  public static class MicrosoftDownloader
  {
    async public static Task<long> GetMicrosoftHomePageSize()
    {
      // local error handling omitted
      var length = 0L;
      HttpClient client = new HttpClient();

      HttpResponseMessage message = await client.GetAsync(
        new Uri("http://www.microsoft.com"));

      using (Stream stream = await message.Content.ReadAsStreamAsync())
      {
        length = stream.Length;
      }
      return (length);
    }
  }
}

The compiler doesn’t like it. The compiler immediately says;

Error    1    Method 'WindowsRuntimeComponent1.MicrosoftDownloader.GetMicrosoftHomePageSize()' has a parameter of type 'System.Threading.Tasks.Task<System.Int64>' in its signature. Although this generic type is not a valid Windows Runtime type, the type or its generic parameters implement interfaces that are valid Windows Runtime types.  Consider changing the type 'Task' in the method signature to one of the following types instead: Windows.Foundation.IAsyncAction, Windows.Foundation.IAsyncOperation, or one of the other Windows Runtime async interfaces. The standard .NET awaiter pattern also applies when consuming Windows Runtime async interfaces. Please see System.Runtime.InteropServices.WindowsRuntime.AsyncInfo for more information about converting managed task objects to Windows Runtime async interfaces.  

Firstly, that is a great, great error message for a compiler to emit. Someone thought long and hard about that one and congratulations to whoever wrote it.

What it’s saying is that I am using async which means that my method is going to return Task<long> and Task<long> is something that is understood by .NET but is meaningless in C++ or in JavaScript and, hence, can’t be dealt with by a WinRT component that spans those worlds.

I need to rethink in terms of IAsyncOperation<long> rather than Task<long>. How to do that? Well, I can’t use async on my function any more because that implies Task or Task<T> so I end up using tasks inside my function if I want to work at the ‘top level’ of things like;

namespace WindowsRuntimeComponent1
{
  using System;
  using System.IO;
  using System.Net.Http;
  using System.Threading.Tasks;
  using Windows.Foundation;

  public static class MicrosoftDownloader
  {
    public static IAsyncOperation<long> GetMicrosoftHomePageSize()
    {
      // local error handling omitted
      HttpClient client = new HttpClient();

      Task<HttpResponseMessage> httpTask = client.GetAsync(
        new Uri("http://www.microsoft.com"));

      Task<Task<Stream>> streamTaskTask = httpTask.ContinueWith(
          httpT =>
          {
            // TODO: check for success etc.
            return (httpT.Result.Content.ReadAsStreamAsync());
          }
      );
      // Unfortunately, we end up with a task in a task...
      Task<Stream> streamTask = TaskExtensions.Unwrap<Stream>(streamTaskTask);

      Task<long> longTask = streamTask.ContinueWith(
        streamT =>
        {
          return (streamT.Result.Length);
        }
      );
      return (longTask.AsAsyncOperation<long>());
    }
  }
}

but that’s a bit tedious as I’ve lost all the joy of being able to lean on async/await and being able to put Task to one side so an easier way of doing it might be just to delegate the work down to a sub-function which isn’t promoted across the WinRT boundary so can continue to rely on async/await and so on as in;

namespace WindowsRuntimeComponent1
{
  using System;
  using System.IO;
  using System.Net.Http;
  using System.Threading.Tasks;
  using Windows.Foundation;

  // .NET on the inside, WinRT on the outside - much nicer :-)
  public static class MicrosoftDownloader
  {
    static async Task<long> InternalGetMicrosoftHomePageSize()
    {
      long length = 0;

      HttpClient client = new HttpClient();

      HttpResponseMessage message = await client.GetAsync(
        new Uri("http://www.microsoft.com"));

      using (Stream stream = await message.Content.ReadAsStreamAsync())
      {
        length = stream.Length;
      }
      return (length);
    }
    public static IAsyncOperation<long> GetMicrosoftHomePageSize()
    {
      return (InternalGetMicrosoftHomePageSize().AsAsyncOperation<long>());
    }
  }

and that feels a whole lot nicer Smile


Posted Fri, Oct 5 2012 3:28 PM by mtaulty
Filed under: , ,

Comments

essays online wrote re: Projecting Async .NET Code from a WinRT Component
on Wed, Oct 10 2012 9:29 PM

Goodd cod! Right. Thanks very helpful!

custom research paper wrote re: Projecting Async .NET Code from a WinRT Component
on Tue, Oct 16 2012 11:32 AM

It's very interesting info! Thanks a lot for it.

mkArtak wrote re: Projecting Async .NET Code from a WinRT Component
on Mon, Oct 22 2012 5:14 PM

Good trick, thanks for sharing.

MrXee wrote re: Projecting Async .NET Code from a WinRT Component
on Fri, Oct 26 2012 10:08 PM

Very helpful! Thanks a lot.

Hans wrote re: Projecting Async .NET Code from a WinRT Component
on Mon, Dec 3 2012 8:28 AM

Could you have use RunSynchronously() instead? Combining multiple Async tasks in a Synchronous way that serve the same purpose.....   Ofcourse you keep the method return the IAsyncOperation<long> asynchronous.

Mike Taulty's Blog wrote await Task, Task.Wait and Friends
on Wed, Jul 3 2013 9:42 AM

I was in one of the threading talks at Build and one of the questions that got raised highlighted that