Click or drag to resize
TaskBlocksUsingTResource Method (FuncTaskTResource, FuncTaskTResource, Task)
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 Using<TResource>(
	Func<Task<TResource>> resource,
	Func<Task<TResource>, Task> body
)
where TResource : IDisposable

Parameters

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

Type Parameters

TResource
The type of resource used within the task and disposed of afterwards.

Return Value

Type: Task
A Task representing the asynchronous operation.
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))
{
    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. No result is return from this operation, as the body of the task block represents an asynchronous operation that does not return a result.

C#
public Task Using(StringBuilder builder)
{
    return TaskBlocks.Using(
        () => AcquireResourceAsync(builder),
        task => task.Result.WriteAsync(SampleText));
}

private Task<StringWriter> AcquireResourceAsync(StringBuilder builder)
{
    // this would generally contain an asynchronous call
    return CompletedTask.FromResult(new StringWriter(builder));
}

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

C#
public async Task UsingAsyncAwait(StringBuilder builder)
{
    using (StringWriter resource = await AcquireResourceAsyncAwait(builder))
    {
        await resource.WriteAsync(SampleText);
    }
}

private async Task<StringWriter> AcquireResourceAsyncAwait(StringBuilder builder)
{
    // this would generally contain an asynchronous call
    return new StringWriter(builder);
}
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