服务总线队列如何将任务<BrokeredMessage>映射到任务<MyClass>
Service Bus Queue how to map Task<BrokeredMessage> to Task<MyClass>
我正在为服务总线队列使用适配器。因此,我不应该为 return 类型使用 Queue 的任何 class。但我坚持使用 ReceiveAsync()
方法。我如何映射 Task<BrokeredMessage> to Task<MyAdapterClass>
?
这是 BrokeredMessage
的适配器 Class
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;
}
谢谢理查德。我刚刚改进了您的答案,并且效果很好。
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;
}
我正在为服务总线队列使用适配器。因此,我不应该为 return 类型使用 Queue 的任何 class。但我坚持使用 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;
}
谢谢理查德。我刚刚改进了您的答案,并且效果很好。
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;
}