2

I'm using adapter for Service Bus Queue. Therefore, I shouldn't use any class of Queue for return type. But I'm stucked on ReceiveAsync() method. How can I map Task<BrokeredMessage> to Task<MyAdapterClass> ?

Here is adapter Class for 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; } }
    }

And I want Method like this

 public  Task<IQueueMessage> ReceiveAsync(TimeSpan serverWaitTime)
        {

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

            return task;
        }

On Driver project I want to use task like this:

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

2 回答 2

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;
}
于 2015-03-21T14:51:39.267 回答
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;
        }
于 2015-03-23T11:48:43.630 回答