Merge .NET 4.0 Tasks / Extensions of Different Types

I am currently implementing System.Web.Http.IActionFilterthat calls an internal service to determine if the current request can continue. The problem I am facing is returning Task<T1>based on the piece of logic enclosed in Task<T2>.

An example may help.

The internal service API is implemented using Tasks. The logic is trivial using .NET 4.5 async / wait:

public async Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
    UserAuthenticationResult authResult = await HitInternalServiceAsync();

    if (!authResult.IsAuthenticated)
    {
        throw new HttpResponseException("User is not authenticated", HttpStatusCode.Unauthorized);
    }

    return await continuation();
}

However, more complicated with the old task API in .NET 4.0 ;

public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
    return HitInternalServiceAsync()
            .ContinueWith(t1 => {
                UserAuthenticationResult authResult = t1.Result;

                if (!authResult.IsAuthenticated)
                {
                    throw new HttpResponseException("User is not authenticated", HttpStatusCode.Unauthorized);
                }

                //Hack hack - this blocks the thread until the task retuned by continuation() completes
                return continuation().Result;
            });
}

The difficult part arises when authentication is successful - I want to wait for the task returned by the continue function.

.NET 4.0 , , continuation(), , API continuation(), .

: .NET 4.0?

API , , .

: , 4.0 - ASP.NET, HttpContext.Current . ...

public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
    Task<UserAuthenticationResult> authResultTask = HitInternalServiceAsync();

    var authResult = authResultTask.Result;

    if (!authResult.IsAuthenticated)
    {
        throw new HttpResponseException("User is not authenticated", HttpStatusCode.Unauthorized);
    }

    return continuation();
}
+3
3

, Result, ContinueWith() Task<Task<HttpResponseMessage>>, Task<HttpResponseMessage> .

, , Task<Task<T>> Task<T>: Unwrap(). , return continuation(); ContinueWith() , Unwrap() .

, ASP.NET, TaskScheduler.FromCurrentSynchronizationContext().

+5

: .NET 4.0?

async/await - # 5.0, .NET 4.5. , .NET 4.5, , .

svick , VS2010 (# 4.0).

, VS11 Beta (# 5.0): async/await, .NET 4.0. .NET 4.0.

+3

(). continue().Wait()

task.wait - .

MSDN, Task.Wait: .

http://msdn.microsoft.com/en-us/library/dd235635.aspx

Folowing , # 5.0 "" "" ?

+1

All Articles