async Task IFilter <ConnectionContext> .Send(ConnectionContext context, IPipe <ConnectionContext> next) { var session = await context.CreateSession(context.CancellationToken).ConfigureAwait(false); var sessionContext = new ActiveMqSessionContext(context, session, context.CancellationToken); void HandleException(Exception exception) { #pragma warning disable 4014 sessionContext.DisposeAsync(CancellationToken.None); #pragma warning restore 4014 } context.Connection.ExceptionListener += HandleException; try { await _pipe.Send(sessionContext).ConfigureAwait(false); } finally { context.Connection.ExceptionListener -= HandleException; await sessionContext.DisposeAsync(CancellationToken.None).ConfigureAwait(false); } await next.Send(context).ConfigureAwait(false); }
void CreateSession(IAsyncPipeContextAgent <SessionContext> asyncContext, CancellationToken cancellationToken) { IPipe <ConnectionContext> connectionPipe = Pipe.ExecuteAsync <ConnectionContext>(async connectionContext => { if (_log.IsDebugEnabled) { _log.DebugFormat("Creating session: {0}", connectionContext.Description); } try { var session = await connectionContext.CreateSession().ConfigureAwait(false); var sessionContext = new ActiveMqSessionContext(connectionContext, session, _host, cancellationToken); await asyncContext.Created(sessionContext).ConfigureAwait(false); await asyncContext.Completed.ConfigureAwait(false); } catch (OperationCanceledException) { await asyncContext.CreateCanceled().ConfigureAwait(false); } catch (Exception exception) { await asyncContext.CreateFaulted(exception).ConfigureAwait(false); } }); _connectionCache.Send(connectionPipe, cancellationToken).ConfigureAwait(false); }
void CreateSession(IAsyncPipeContextAgent <SessionContext> asyncContext, CancellationToken cancellationToken) { IPipe <ConnectionContext> connectionPipe = Pipe.ExecuteAsync <ConnectionContext>(async connectionContext => { try { var session = await connectionContext.CreateSession(cancellationToken).ConfigureAwait(false); LogContext.Debug?.Log("Created session: {Host}", connectionContext.Description); var sessionContext = new ActiveMqSessionContext(connectionContext, session, cancellationToken); void HandleException(Exception exception) { #pragma warning disable 4014 sessionContext.DisposeAsync(CancellationToken.None); #pragma warning restore 4014 } connectionContext.Connection.ExceptionListener += HandleException; #pragma warning disable 4014 // ReSharper disable once MethodSupportsCancellation asyncContext.Completed.ContinueWith(task => #pragma warning restore 4014 { connectionContext.Connection.ExceptionListener -= HandleException; }); await asyncContext.Created(sessionContext).ConfigureAwait(false); await asyncContext.Completed.ConfigureAwait(false); } catch (OperationCanceledException) { await asyncContext.CreateCanceled().ConfigureAwait(false); } catch (Exception exception) { LogContext.Error?.Log(exception, "Create session failed: {Host}", connectionContext.Description); 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); }
void CreateSession(IAsyncPipeContextAgent <SessionContext> asyncContext, CancellationToken cancellationToken) { IPipe <ConnectionContext> connectionPipe = Pipe.ExecuteAsync <ConnectionContext>(async connectionContext => { if (_log.IsDebugEnabled) { _log.DebugFormat("Creating session: {0}", connectionContext.Description); } try { var session = await connectionContext.CreateSession().ConfigureAwait(false); var sessionContext = new ActiveMqSessionContext(connectionContext, session, cancellationToken); void HandleException(Exception exception) { var disposeAsync = sessionContext.DisposeAsync(CancellationToken.None); } connectionContext.Connection.ExceptionListener += HandleException; #pragma warning disable 4014 asyncContext.Completed.ContinueWith(task => #pragma warning restore 4014 { connectionContext.Connection.ExceptionListener -= HandleException; }); await asyncContext.Created(sessionContext).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 session = await context.CreateSession().ConfigureAwait(false); var sessionContext = new ActiveMqSessionContext(context, session, _host, context.CancellationToken); try { await _pipe.Send(sessionContext).ConfigureAwait(false); } finally { await sessionContext.DisposeAsync(CancellationToken.None).ConfigureAwait(false); } await next.Send(context).ConfigureAwait(false); }