【发布时间】:2017-11-17 03:19:33
【问题描述】:
以下是在本地 SF 集群中运行并侦听服务总线队列的无状态服务侦听器代码。
我想要的只是在一个永远在线的 SF 服务中持续监听队列命令。欢迎提供代码改进提示!
问题 #1
Abort() 被连续调用,有效地关闭了我的连接。是什么导致了这种行为,我该如何解决?在我的理解中,Abort() 应该只在未处理的异常或强制关闭的情况下被调用,我都不知道。
奖金问题
假设我们从 Abort() 中注释掉 CloseClient 调用,这样可以正确处理我们的队列。在第一条消息之后,CancellationToken 的 WaitHandle 被标记为已释放,并将其传递给我的回调会引发异常。这是什么原因造成的?
感谢您的帮助!
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ServiceBus.Messaging;
using Microsoft.ServiceFabric.Services.Communication.Runtime;
namespace Common
{
public class QueueListener : ICommunicationListener
{
private readonly string _connectionString;
private readonly string _path;
private readonly Action<BrokeredMessage> _callback;
private QueueClient _client;
public QueueListener(string connectionString, string path, Action<BrokeredMessage> callback)
{
// Set field values
_connectionString = connectionString;
_path = path;
// Save callback action
_callback = callback;
}
public Task<string> OpenAsync(CancellationToken cancellationToken)
{
// Connect to subscription
_client = QueueClient.CreateFromConnectionString(_connectionString, _path);
// Configure the callback options
var options = new OnMessageOptions
{
AutoComplete = false
};
// Catch and throw exceptions
options.ExceptionReceived += (sender, args) => throw args.Exception;
// Wire callback on message receipt
_client.OnMessageAsync(message =>
{
return Task.Run(() => _callback(message), cancellationToken)
.ContinueWith(task =>
{
if (task.Status == TaskStatus.RanToCompletion)
message.CompleteAsync();
else
message.AbandonAsync();
}, cancellationToken);
}, options);
return Task.FromResult(_client.Path);
}
public Task CloseAsync(CancellationToken cancellationToken)
{
CloseClient();
return Task.FromResult(_client.Path);
}
public void Abort()
{
CloseClient();
}
private void CloseClient()
{
// Make sure client is still open
if (_client == null || _client.IsClosed)
return;
// Close connection
_client.Close();
_client = null;
}
}
}
【问题讨论】:
标签: c# azure azure-service-fabric azureservicebus azure-servicebus-queues