async Task SendUsingNewModel(IPipe <ModelContext> modelPipe, ModelScope scope, CancellationToken cancellationToken) { IPipe <ConnectionContext> connectionPipe = Pipe.ExecuteAsync <ConnectionContext>(async connectionContext => { IModel model = await connectionContext.CreateModel().ConfigureAwait(false); EventHandler <ShutdownEventArgs> modelShutdown = null; modelShutdown = (obj, reason) => { model.ModelShutdown -= modelShutdown; Interlocked.CompareExchange(ref _scope, null, scope); scope.Close(); }; model.ModelShutdown += modelShutdown; var modelContext = new RabbitMqModelContext(connectionContext, model, connectionContext.CancellationToken); scope.Connected(modelContext); try { using (SharedModelContext context = await scope.Attach(cancellationToken).ConfigureAwait(false)) { await modelPipe.Send(context).ConfigureAwait(false); } } catch (Exception ex) { if (_log.IsDebugEnabled) { _log.Debug("The existing model usage threw an exception", ex); } Interlocked.CompareExchange(ref _scope, null, scope); scope.Close(); throw; } }); try { await _connectionCache.Send(connectionPipe, new CancellationToken()).ConfigureAwait(false); } catch (Exception exception) { if (_log.IsDebugEnabled) { _log.Debug("The connection threw an exception", exception); } Interlocked.CompareExchange(ref _scope, null, scope); throw; } }
async Task SendUsingNewModel(IPipe<ModelContext> modelPipe, ModelScope scope, CancellationToken cancellationToken) { IPipe<ConnectionContext> connectionPipe = Pipe.ExecuteAsync<ConnectionContext>(async connectionContext => { IModel model = await connectionContext.CreateModel().ConfigureAwait(false); EventHandler<ShutdownEventArgs> modelShutdown = null; modelShutdown = (obj, reason) => { model.ModelShutdown -= modelShutdown; Interlocked.CompareExchange(ref _scope, null, scope); scope.Close(); }; model.ModelShutdown += modelShutdown; var modelContext = new RabbitMqModelContext(connectionContext, model, connectionContext.CancellationToken); scope.Connected(modelContext); try { using (SharedModelContext context = await scope.Attach(cancellationToken).ConfigureAwait(false)) { await modelPipe.Send(context).ConfigureAwait(false); } } catch (Exception ex) { if (_log.IsDebugEnabled) _log.Debug("The existing model usage threw an exception", ex); Interlocked.CompareExchange(ref _scope, null, scope); scope.Close(); throw; } }); try { await _connectionCache.Send(connectionPipe, new CancellationToken()).ConfigureAwait(false); } catch (Exception exception) { if (_log.IsDebugEnabled) _log.Debug("The connection threw an exception", exception); Interlocked.CompareExchange(ref _scope, null, scope); throw; } }
async Task SendUsingExistingModel(IPipe <ModelContext> modelPipe, ModelScope existingScope, CancellationToken cancellationToken) { try { using (SharedModelContext context = await existingScope.Attach(cancellationToken).ConfigureAwait(false)) { await modelPipe.Send(context).ConfigureAwait(false); } } catch (Exception ex) { if (_log.IsDebugEnabled) { _log.Debug("The existing model usage threw an exception", ex); } Interlocked.CompareExchange(ref _scope, null, existingScope); existingScope.Close(); throw; } }
async Task SendUsingExistingModel(IPipe<ModelContext> modelPipe, ModelScope existingScope, CancellationToken cancellationToken) { try { using (SharedModelContext context = await existingScope.Attach(cancellationToken).ConfigureAwait(false)) { await modelPipe.Send(context).ConfigureAwait(false); } } catch (Exception ex) { if (_log.IsDebugEnabled) _log.Debug("The existing model usage threw an exception", ex); Interlocked.CompareExchange(ref _scope, null, existingScope); existingScope.Close(); throw; } }