我正在为服务总线队列使用适配器。因此,我不应该将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;
        }


在Driver项目中,我想使用以下任务:

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

最佳答案

我会尝试在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;
}

09-05 16:54