/// <summary> /// Sends a message asynchronously. /// </summary> /// <param name="sender">The link.</param> /// <param name="message">The message.</param> /// <returns></returns> public static async Task SendAsync(this SenderLink sender, Message message) { DeliveryState txnState = null; #if NETFX || NETFX40 txnState = await TaskExtensions.GetTransactionalStateAsync(sender); #endif TaskCompletionSource <object> tcs = new TaskCompletionSource <object>(); sender.Send( message, txnState, (m, o, s) => { var t = (TaskCompletionSource <object>)s; if (o.Descriptor.Code == Codec.Accepted.Code) { t.SetResult(null); } else if (o.Descriptor.Code == Codec.Rejected.Code) { t.SetException(new AmqpException(((Rejected)o).Error)); } else { t.SetException(new AmqpException(ErrorCode.InternalError, o.Descriptor.Name)); } }, tcs); await tcs.Task; }
internal async Task SendInternalAsync(Message message, int timeoutMilliseconds) { DeliveryState txnState = null; #if NETFX || NETFX40 txnState = await TaskExtensions.GetTransactionalStateAsync(this); #endif TaskCompletionSource <object> tcs = new TaskCompletionSource <object>(); this.Send( message, txnState, (l, m, o, s) => { var t = (TaskCompletionSource <object>)s; if (o.Descriptor.Code == Codec.Accepted.Code) { t.SetResult(null); } else if (o.Descriptor.Code == Codec.Rejected.Code) { t.SetException(new AmqpException(((Rejected)o).Error)); } else if (o.Descriptor.Code == Codec.Released.Code) { t.SetException(new AmqpException(ErrorCode.MessageReleased, null)); } else { t.SetException(new AmqpException(ErrorCode.InternalError, o.Descriptor.Name)); } }, tcs); await tcs.Task; }
/// <summary> /// Sends a message asynchronously. /// </summary> /// <param name="message">The message to send.</param> /// <param name="timeout">The time to wait for the task to complete.</param> /// <returns>A Task for the asynchronous send operation.</returns> public async Task SendAsync(Message message, TimeSpan timeout) { DeliveryState txnState = null; #if NETFX || NETFX40 txnState = await TaskExtensions.GetTransactionalStateAsync(this); #endif try { await new SendTask(this, message, txnState, timeout).Task; } catch (TimeoutException) { this.OnTimeout(message); throw; } }