我正在从网络角色向工作角色发送消息。早些时候,这些消息被正确和立即接收,但现在不知从何处没有收到一些消息,即使所有消息的内容都是相同的。就像我现在发送一条消息一样,它没有收到,但在我发送另一条消息之后立即收到最新的一条。之前没有收到的消息,在几秒钟后突然收到,或者有时会随着生存时间到期而变成死信消息。
我无法弄清楚问题可能是什么,有什么想法吗?或者这种行为在服务总线上是正常的吗?
这就是我在工作角色中接收消息的方式
编辑:
public override void Run()
{
while (!IsStopped)
{
try
{
if (BroadcastReceived)
{
BroadcastReceived = false;
// Receive the message from Web Role to upload the broadcast to queue
BroadcastClient.BeginReceive(OnWebRoleMessageReceived, null);
}
if (SignalRMessageReceived)
{
SignalRMessageReceived = false;
// Receive the message from SignalR BroadcastHub
SignalRClient.BeginReceive(OnSignalRMessageReceived, null);
}
if (SignalRFirstTimeMessageReceived)
{
SignalRFirstTimeMessageReceived = false;
// Receive the message from SignalR BroadcastHub
SignalRFirstTimeClient.BeginReceive(OnSignalRFirstTimeMessageReceived, null);
}
}
}
public void OnWebRoleMessageReceived(IAsyncResult iar)
{
BrokeredMessage receivedBroadcastMessage = null;
receivedBroadcastMessage = BroadcastClient.EndReceive(iar);
if (receivedBroadcastMessage != null)
{
// Process the message
receivedBroadcastMessage.Complete();
}
BroadcastReceived = true;
}
更新 :
BroadcastClient = CloudStorageHelper.GetServiceBusQueueClient(Queuenames.ApiToWorkerRole);
public static QueueClient GetServiceBusQueueClient(string queuename)
{
string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
if (!namespaceManager.QueueExists(queuename))
{
namespaceManager.CreateQueue(queuename);
}
QueueClient Client = QueueClient.CreateFromConnectionString(connectionString, queuename);
return Client;
}
网络角色:
var queueClient = CloudStorageHelper.GetServiceBusQueueClient(Queuenames.ApiToWorkerRole);
//for testing purposes
record.Username = "owner";
record.Channel = "World";
record.Title = "Sample";
record.Duration = 10;
BrokeredMessage message = new BrokeredMessage(record);
queueClient.Send(message);