Tag Archives: C#5.0

Introducing TPL Async Generator

I am playing with Azure last months. I love Azure. Really breath of fresh air last days… But there is small funny issue… Client libraries are async. But async is implement with old IAsyncResult model. Uuuu. Ugly. Let’s fix this.

I implemented small T4 template that actually

  1. looks for all methods with Begin
  2. and generate extension methods with TPL async implementation

Usage is simple,

  1. Install from nuget
  2. Modify example template
  3. Press save
  4. Enjoy!

TL;DR

For example QueueClient, it have methods like:

public IAsyncResult BeginReceive(
	TimeSpan serverWaitTime,
	AsyncCallback callback,
	Object state
)

AsyncGenerator will generate:

public static Task<BrokeredMessage> ReceiveAsync(this MessageReceiver @this
                                            ,TimeSpan serverWaitTime)
{
      if (@this == null) throw new ArgumentNullException("this");
      return Task<BrokeredMessage>.Factory.FromAsync(
                @this.BeginReceive, @this.EndReceive,
                serverWaitTime, null);
}

Now you can use azure in normal C# 5.0 way:

var message = await queue.ReceiveAsync();
if (message != null)
{
     Console.WriteLine(message);
     await message.CompleteAsync();
}

 

Enjoy!

Advertisements
Tagged , , , , ,