Service Bus Queue如何将Task <brokeredmessage>映射到Task <myclass> </myclass> </brokeredmessage>

时间:2015-03-20 15:59:24

标签: c# mapping task azure-servicebus-queues brokeredmessage

我正在使用适配器进行服务总线队列。因此,我不应该使用任何类的Queue作为返回类型。但我坚持使用ReceiveAsync()方法。如何映射Task<BrokeredMessage> to Task<MyAdapterClass>

以下是BrokeredMessage

的适配器类
public class QueueMessage : IQueueMessage
    {
        private BrokeredMessage _message;
        public T Body<T>()
        {
            T result = default(T);
            if (_message != null)
                result = _message.GetBody<T>();
            return result;

        }
        public BrokeredMessage Message { set { _message = value; } }

        public string Label
        {
            get
            {
                var result = "";
                if (_message != null)
                    result = _message.Label;
                return result;
            }
        }

        public void MoveToDeadLetter()
        {
            if (_message != null)
                _message.DeadLetter();
        }

        public void Complete()
        {
            if (_message != null)
                _message.Complete();

        }


        public async void CompleteAsync()
        {
            if (_message != null)
                await _message.CompleteAsync();

        }

        public async void AbandonAsync()
        {
            if (_message != null)
                await _message.AbandonAsync();

        }
        public void Abandon()
        {
            if (_message != null)
                _message.Abandon();
        }

        public string MessageId
        {
            get
            {
                return _message == null ? null : _message.MessageId;
            }
            set { if (_message != null) _message.MessageId = value; }
        }

        public string CorrelationId
        {
            get
            {
                return _message == null ? null : _message.CorrelationId;
            }
            set { if (_message != null) _message.CorrelationId = value; }
        }

        public int DeliveryCount { get { return _message == null ? -1 : _message.DeliveryCount; } }
    }

我想要像这样的方法

 public  Task<IQueueMessage> ReceiveAsync(TimeSpan serverWaitTime)
        {

            Task<QueueMessage> task= QueueClient.ReceiveAsync(serverWaitTime);

            return task;
        }

在驱动程序项目中我想使用这样的任务:

 var task1 = _queueAdapter.ReceiveAsync(new TimeSpan(200)).ContinueWith(ReadMessageAsync);

2 个答案:

答案 0 :(得分:1)

我会在QueueClient.ReceiveAsync()上尝试继续。见https://msdn.microsoft.com/en-us/library/vstudio/ee372288(v=vs.100).aspx

public Task<IQueueMessage> ReceiveAsync(TimeSpan serverWaitTime)
{
    Task<QueueMessage> task = QueueClient
                              .ReceiveAsync(serverWaitTime)
                              .ContinueWith(bm => new QueueMessage{Message = bm});
    return task;
}

答案 1 :(得分:1)

谢谢理查德。我刚刚改进了你的答案,这很好用。

public async Task<IQueueMessage> ReceiveAsycn(TimeSpan serverWaitTime)
        {
            var task = QueueClient
                                      .ReceiveAsync(serverWaitTime)
                                      .ContinueWith(bm => bm.Result==null ? null : new QueueMessage { Message=bm.Result});
            return await task;
        }