in iothub/service/src/Messaging/ServiceClient.cs [560:624]
public virtual async Task SendAsync(string deviceId, string moduleId, Message message, TimeSpan? timeout = null)
{
if (Logging.IsEnabled)
Logging.Enter(this, $"Sending message with Id [{message?.MessageId}] for device {deviceId}, module {moduleId}", nameof(SendAsync));
if (string.IsNullOrWhiteSpace(deviceId))
{
throw new ArgumentNullException(nameof(deviceId));
}
if (string.IsNullOrWhiteSpace(moduleId))
{
throw new ArgumentNullException(nameof(moduleId));
}
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
if (_clientOptions?.SdkAssignsMessageId == SdkAssignsMessageId.WhenUnset && message.MessageId == null)
{
message.MessageId = Guid.NewGuid().ToString();
}
if (message.IsBodyCalled)
{
message.ResetBody();
}
timeout ??= _operationTimeout;
using AmqpMessage amqpMessage = MessageConverter.MessageToAmqpMessage(message);
amqpMessage.Properties.To = "/devices/" + WebUtility.UrlEncode(deviceId) + "/modules/" + WebUtility.UrlEncode(moduleId) + "/messages/deviceBound";
try
{
SendingAmqpLink sendingLink = await GetSendingLinkAsync().ConfigureAwait(false);
Outcome outcome = await sendingLink
.SendMessageAsync(
amqpMessage,
IotHubConnection.GetNextDeliveryTag(ref _sendingDeliveryTag),
AmqpConstants.NullBinary,
timeout.Value)
.ConfigureAwait(false);
if (Logging.IsEnabled)
Logging.Info(this, $"Outcome was: {outcome?.DescriptorName}", nameof(SendAsync));
if (outcome.DescriptorCode != Accepted.Code)
{
throw AmqpErrorMapper.GetExceptionFromOutcome(outcome);
}
}
catch (Exception ex) when (!ex.IsFatal())
{
if (Logging.IsEnabled)
Logging.Error(this, $"{nameof(SendAsync)} threw an exception: {ex}", nameof(SendAsync));
throw AmqpClientHelper.ToIotHubClientContract(ex);
}
finally
{
if (Logging.IsEnabled)
Logging.Exit(this, $"Sending message with Id [{message?.MessageId}] for device {deviceId}, module {moduleId}", nameof(SendAsync));
}
}