Click or drag to resize
TaskBlocksUsingTResource, TResult Method (FuncTaskTResource, FuncTaskTResource, TaskTResult)
Provides support for resource cleanup in asynchronous code where the / keywords are not available.

Namespace: Rackspace.Threading
Assembly: Rackspace.Threading (in Rackspace.Threading.dll) Version: 2.1.0-beta1
Syntax
public static Task<TResult> Using<TResource, TResult>(
	Func<Task<TResource>> resource,
	Func<Task<TResource>, Task<TResult>> body
)
where TResource : IDisposable

Parameters

resource
Type: SystemFuncTaskTResource
A function which acquires the resource used during the execution of the task.
body
Type: SystemFuncTaskTResource, TaskTResult
The continuation function which provides the TaskTResult which acts as the body of the using block.

Type Parameters

TResource
The type of resource used within the task and disposed of afterwards.
TResult
The type of the result produced by the continuation TaskTResult.

Return Value

Type: TaskTResult
A Task representing the asynchronous operation. When the task completes successfully, the Result property will contain the result provided by the Result property of the task returned from body.
Exceptions
ExceptionCondition
ArgumentNullException

If resource is .

-or-

If body is .

InvalidOperationException

If resource returns .

Remarks

This code implements support for the following construct without requiring the use of /.

C#
using (IDisposable disposable = await resource().ConfigureAwait(false))
{
    return await body(disposable).ConfigureAwait(false);
}

This method expands on the using statement provided by C# by implementing support for IAsyncDisposable as described in IAsyncDisposable, using statements, and async/await.

Note Notes to Callers

If the resource function throws an exception, or if it returns , or if the TaskTResult it returns does not complete successfully, the resource will not be acquired by this method. In either of these situations the caller is responsible for ensuring the resource function cleans up any resources it creates.

Examples

The following example asynchronously acquires a resource by calling the user method AcquireResourceAsync. The resource will be disposed after the body executes, prior to returning the result of the body.

C#
public Task<string> UsingWithResult()
{
    return TaskBlocks.Using(
        () => AcquireResourceAsync(),
        task => task.Result.ReadToEndAsync());
}

private Task<StringReader> AcquireResourceAsync()
{
    // this would generally contain an asynchronous call
    return CompletedTask.FromResult(new StringReader("Text to read"));
}

For reference, the following example demonstrates a (nearly) equivalent implementation of this behavior using the / operators.

C#
public async Task<string> UsingWithResultAsyncAwait()
{
    using (StringReader resource = await AcquireResourceAsyncAwait())
    {
        return await resource.ReadToEndAsync();
    }
}

private async Task<StringReader> AcquireResourceAsyncAwait()
{
    // this would generally contain an asynchronous call
    return new StringReader("Text to read");
}
Version Information

.NET for Windows Store apps

Supported in: Windows 8

.NET Framework

Supported in: 4.5, 4.0, 3.5

Portable Class Library

Supported in: Portable Class Library (Legacy), Portable Class Library

Threading Library

Supported in: 2.1, 2.0, 1.1, 1.0
See Also