我正在使用服务总线在 Web 角色和工作角色之间进行通信,但它给了我一些不稳定的结果。我已将我的云项目部署到 azure。令人惊讶的是,当我只运行已部署的项目时,服务总线工作正常,但是当我也在使用服务总线的本地仿真器上运行相同的项目时,服务总线出现故障,因为所有消息都出现死信或不被工作者角色接收。这是服务总线的正常行为吗?如果不是,可能是什么问题,我该如何解决?
这对我来说很烦人,因为现在当我在本地模拟器上运行我的项目时,服务总线永远无法正常工作。
这就是发送和接收消息的方式,但我想问题可能不在其中,因为当服务总线仅由部署的项目使用时它工作正常。
工人角色:
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;
}
网络角色:
var queueClient = CloudStorageHelper.GetServiceBusQueueClient(Queuenames.ApiToWorkerRole);
BrokeredMessage message = new BrokeredMessage(record);
queueClient.Send(message);
如何创建服务总线队列:
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;
}