Dmitry Shechtman's Blog

November 30, 2014

AsyncActivator: Targeting Multiple Platforms

This is the third article in a series concerning AsyncActivator, a generalization of the .NET Asynchronous Factory pattern:

  1. Basics
  2. Cancellation
  3. Portability
  4. Silverlight
  5. Dependency injection

The Problem

Two previous installments discussed an implementation of AsyncActivator, later extended to support cancellation. Today we’re going to make it Portable Class Library (PCL) compliant.

AsyncActivator works great on the full-blown .NET Framework 4.5, but what if we wanted to use it, say, on WinRT?

We’ll start by creating a new Class Library (Portable) project and adding our existing files to it:

  • AsyncActivator.cs
  • AsyncInitBase.cs
  • CancelableAsyncInitBase.cs
  • IAsyncInit.cs
  • ICancelableAsyncInit.cs

Make sure the Silverlight box is unchecked, and it builds without a hitch. However, trying to run our UniversalAnswerService example results in:

Unhandled Exception: System.MissingMethodException: Constructor on type ‘UniversalAnswerService’ not found.

How’s that possible? We didn’t even touch UniversalAnswerService!

(more…)

Advertisements

November 27, 2014

AsyncActivator: Handling Cancellation

This is the second article in a series concerning AsyncActivator, a generalization of the .NET Asynchronous Factory pattern:

  1. Basics
  2. Cancellation
  3. Portability
  4. Silverlight
  5. Dependency injection

The Problem

Recall UniversalAnswerService from the previous article:

class UniversalAnswerService : AsyncInitBase<UniversalAnswerService>
{
    private UniversalAnswerService()
    {
    }

    protected override async Task InitAsync()
    {
        await Task.Delay(TimeSpan.FromDays(7500000 * 365.25));
        Answer = 42;
    }

    public int Answer { get; private set; }
}

Unlike Stephen Cleary’s original example, our implementation conveys a more realistic delay. Let’s assume that we’re able to come up with a server that’s actually capable of running this code. We’re now bound to deal with particularly impatient end users refusing to wait for the computation to complete. What we need is a way for our service to respond to cancellation requests.

Although this is a somewhat contrived example, cancellation is a very serious issue that needs to be addressed in most real-life scenarios.

(more…)

November 24, 2014

AsyncActivator: Yet Another Take on the .NET Async Constructor Problem

Filed under: AsyncActivator — Tags: , , , , , , , , — Dmitry Shechtman @ 21:18

This is the first article in a series concerning AsyncActivator, a generalization of the .NET Asynchronous Factory pattern:

  1. Basics
  2. Cancellation
  3. Portability
  4. Silverlight
  5. Dependency injection

If you’re in a hurry, feel free to

Update 2014-11-30: The second and the third article in the series provide important updates. Please consider reading them before using code from this article.

The Problem

The Task-based Asynchronous Pattern introduced in .NET Framework 4.5 provides developers with a streamlined syntax for consuming asynchronous tasks and operations. WinRT takes it to the extreme by effectively forcing the developers to incorporate TAP in their code.

This leaves us with the problem of asynchronous constructors, or, more precisely, lack thereof. Stephen Cleary had a great write-up a while ago (more recently republished as part of his MSDN Magazine series) with a couple of solutions to this problem.

(more…)

Create a free website or blog at WordPress.com.