private void OnClosed(IAmqpObject sender, Error error) { if (sender.Error != null) { Error = new DomainEventError(); Error.Condition = sender.Error.Condition.ToString(); Error.Description = sender.Error.Description; } Closed?.Invoke(this, Error); }
private async Task InnerSendAsync(Message message, EventProperties properties) { using (Logger.BeginScope(new Dictionary <string, object> { ["CorrelationId"] = message.Properties.CorrelationId, ["MessageId"] = message.Properties.MessageId, ["MessageType"] = message.Properties.GroupId })) { Logger.LogTrace($"Publishing message {message.Properties.MessageId} to {properties.Address} with body: {message.Body}"); var disconnectAfter = false; if (conn == null) { Connect(); disconnectAfter = true; } var session = new Session(conn); var attach = new Attach() { Target = new Target() { Address = properties.Address, Durable = Settings.Durable }, Source = new Source() }; var sender = new SenderLink(session, Settings.AppName + Guid.NewGuid().ToString(), attach, null); sender.Closed += OnClosed; try { await sender.SendAsync(message).ConfigureAwait(false); Logger.LogInformation($"Published message {message.Properties.MessageId}"); } finally { if (sender.Error != null) { Error = new DomainEventError(); Error.Condition = sender.Error.Condition.ToString(); Error.Description = sender.Error.Description; Closed?.Invoke(this, Error); } if (disconnectAfter) { if (!sender.IsClosed) { await sender.CloseAsync(TimeSpan.FromSeconds(5)).ConfigureAwait(false); } await session.CloseAsync().ConfigureAwait(false); await session.Connection.CloseAsync().ConfigureAwait(false); } } } }
protected override async Task SendAsync(Message message, EventProperties properties) { var disconnectAfter = false; if (conn == null) { Connect(); disconnectAfter = true; } var session = new Session(conn); var attach = new Attach() { Target = new Target() { Address = properties.Address, Durable = Settings.Durable }, Source = new Source() }; var sender = new SenderLink(session, Settings.AppName + Guid.NewGuid().ToString(), attach, null); sender.Closed += OnClosed; try { await sender.SendAsync(message).ConfigureAwait(false); Logger.LogInformation($"Published message {message.Properties.MessageId}"); } finally { if (sender.Error != null) { Error = new DomainEventError(); Error.Condition = sender.Error.Condition.ToString(); Error.Description = sender.Error.Description; Closed?.Invoke(this, Error); } if (disconnectAfter) { if (!sender.IsClosed) { await sender.CloseAsync(TimeSpan.FromSeconds(5)).ConfigureAwait(false); } await session.CloseAsync().ConfigureAwait(false); await session.Connection.CloseAsync().ConfigureAwait(false); } } }