void CreateModel(IAsyncPipeContextAgent <ClientContext> asyncContext, CancellationToken cancellationToken) { IPipe <ConnectionContext> connectionPipe = Pipe.ExecuteAsync <ConnectionContext>(async connectionContext => { try { var amazonSqs = await connectionContext.CreateAmazonSqs().ConfigureAwait(false); var amazonSns = await connectionContext.CreateAmazonSns().ConfigureAwait(false); var modelContext = new AmazonSqsClientContext(connectionContext, amazonSqs, amazonSns, cancellationToken); await asyncContext.Created(modelContext).ConfigureAwait(false); await asyncContext.Completed.ConfigureAwait(false); } catch (OperationCanceledException) { await asyncContext.CreateCanceled().ConfigureAwait(false); } catch (Exception exception) { await asyncContext.CreateFaulted(exception).ConfigureAwait(false); } }); var connectionTask = _connectionContextSupervisor.Send(connectionPipe, cancellationToken); Task NotifyCreateCanceled(Task task) => asyncContext.CreateCanceled(); connectionTask.ContinueWith(NotifyCreateCanceled, TaskContinuationOptions.OnlyOnCanceled); Task NotifyCreateFaulted(Task task) => asyncContext.CreateFaulted(task.Exception); connectionTask.ContinueWith(NotifyCreateFaulted, TaskContinuationOptions.OnlyOnFaulted); }
async Task IFilter <ConnectionContext> .Send(ConnectionContext context, IPipe <ConnectionContext> next) { var amazonSqs = await context.CreateAmazonSqs().ConfigureAwait(false); var amazonSns = await context.CreateAmazonSns().ConfigureAwait(false); var modelContext = new AmazonSqsClientContext(context, amazonSqs, amazonSns, context.CancellationToken); try { await _pipe.Send(modelContext).ConfigureAwait(false); } finally { await modelContext.DisposeAsync(CancellationToken.None).ConfigureAwait(false); } await next.Send(context).ConfigureAwait(false); }