C# 服务总线如何在服务器重新启动后重新连接(非azure)
我在本地使用服务总线1.1,我正在构建一个WindowsService,它应该只监听在另一台机器上运行的特定队列 WindowsService连接到SB的代码如下所示:C# 服务总线如何在服务器重新启动后重新连接(非azure),c#,windows-services,servicebus,C#,Windows Services,Servicebus,我在本地使用服务总线1.1,我正在构建一个WindowsService,它应该只监听在另一台机器上运行的特定队列 WindowsService连接到SB的代码如下所示: MessagingFactory _receiverFactory = MessagingFactory.CreateFromConnectionString(sbConnectionString); MessageReceiver _messageReceiver = _receiverFactory.CreateMessag
MessagingFactory _receiverFactory = MessagingFactory.CreateFromConnectionString(sbConnectionString);
MessageReceiver _messageReceiver = _receiverFactory.CreateMessageReceiver(listeningQueue);
BrokeredMessage brokeredMessage = _messageReceiver.Receive(TimeSpan.FromSeconds(60));
在我重新启动服务器之前,一切正常:我注意到,如果WindowsService在Service Bus的所有服务可用之前启动,我的应用程序将永远无法连接,直到我手动重新启动WindowsService
实际上,如果在我构建MessagingFactory时SB没有完全运行,那么接收指令将始终失败,即使SB服务在第二次返回
我试图截获异常(MessagingCommunicationException)并重建MessagingFactory,但根本不起作用,重新连接的唯一方法是重新启动WindowsService
有什么建议吗
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using Polly;
private static void KeepReadingTheQueue(QueueClient qc)
{
int retries = 0;
int eventualFailures = 0;
Policy pol = Policy.Handle<System.OperationCanceledException>(ex => null != ex)
.Or<UnauthorizedAccessException>(ex => null != ex)
.Or<Microsoft.ServiceBus.Messaging.MessagingCommunicationException>()
.Or<Microsoft.ServiceBus.Messaging.MessageLockLostException>()
.WaitAndRetryForever(
sleepDurationProvider: attempt => TimeSpan.FromMilliseconds(500), // Wait 500ms between each try.
onRetry: (exception, calculatedWaitDuration, context) => // Capture some info for logging!
{
ReportDuplicatesOrMissing("Policy.OnRetry", batchNumbers);
// This is your new exception handler!
// Tell the user what they've won!
Console.WriteLine("Log, then retry: " + exception.Message, ConsoleColor.Yellow);
retries++;
});
int i = 0;
int receiveBatchSize = 1; /* i have this in a custom settings class, hard coded here */
while (true)
{
i++;
ReportDuplicatesOrMissing("while.true", batchNumbers);
try
{
// Retry the following call according to the policy - 15 times.
pol.Execute(() =>
{
IEnumerable<BrokeredMessage> messages = null;
if (receiveBatchSize <= 1)
{
// Receive the message from the queue
BrokeredMessage receivedMessage = qc.Receive();
List<BrokeredMessage> listMsgs = new List<BrokeredMessage>();
listMsgs.Add(receivedMessage);
messages = listMsgs as IEnumerable<BrokeredMessage>;
}
else
{
messages = qc.ReceiveBatch(receiveBatchSize);
int count = messages.Count();
if (count > 0)
{
Console.WriteLine("ReceiveBatch.Count='{0}'", count);
}
}
foreach (BrokeredMessage receivedMessage in messages)
{
if (receivedMessage != null)
{
MyPayLoadObject concreteMsgObject = receivedMessage.GetBody<MyPayLoadObject>();
receivedMessage.Complete();
Console.WriteLine(@"Message received: {0}", concreteMsgObject);
}
else
{
Console.WriteLine(@"No new messages in the queue");
Thread.Sleep(1000);
}
Thread.Sleep(100);
}
});
}
catch (Exception e)
{
Console.WriteLine("Request " + i + " eventually failed with: " + e.Message, ConsoleColor.Red);
eventualFailures++;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统线程;
使用Microsoft.ServiceBus;
使用Microsoft.ServiceBus.Messaging;
使用Polly;
private static void保留对队列进行排序(QueueClient qc)
{
int重试次数=0;
int eventualFailures=0;
Policy pol=Policy.Handle(ex=>null!=ex)
.或(ex=>null!=ex)
.或()
.或()
.等待并重试(
sleepDurationProvider:trument=>TimeSpan.FromMillicles(500),//每次尝试之间等待500毫秒。
onRetry:(异常,calculatedWaitDuration,context)=>//捕获一些日志信息!
{
ReportDuplicatesOrMissing(“Policy.OnRetry”,BatchNumber);
//这是您的新异常处理程序!
//告诉用户他们赢了什么!
WriteLine(“记录,然后重试:+异常。消息,控制台颜色。黄色”);
重试++;
});
int i=0;
int receiveBatchSize=1;/*我在一个自定义设置类中有它,硬编码在这里*/
while(true)
{
i++;
ReportDuplicatesOrMissing(“while.true”,batchNumber);
尝试
{
//根据策略重试以下呼叫-15次。
pol.Execute(()=>
{
IEnumerable messages=null;
如果(接收批次大小0)
{
WriteLine(“ReceiveBatch.Count='{0}',Count);
}
}
foreach(消息中的BrokeredMessage receivedMessage)
{
if(receivedMessage!=null)
{
MyPayLoadObject concreteMsgObject=receivedMessage.GetBody();
receivedMessage.Complete();
WriteLine(@“收到的消息:{0}”,concreteMsgObject);
}
其他的
{
Console.WriteLine(@“队列中没有新消息”);
睡眠(1000);
}
睡眠(100);
}
});
}
捕获(例外e)
{
WriteLine(“请求”+i+”最终失败,为:“+e.消息,控制台颜色.Red”);
最终失败++;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="2.0.1.0" targetFramework="net45" />
<package id="Polly" version="4.3.0" targetFramework="net45" />
<package id="WindowsAzure.ServiceBus" version="2.1.4.0" targetFramework="net45" />
</packages>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using Polly;
private static void KeepReadingTheQueue(QueueClient qc)
{
int retries = 0;
int eventualFailures = 0;
Policy pol = Policy.Handle<System.OperationCanceledException>(ex => null != ex)
.Or<UnauthorizedAccessException>(ex => null != ex)
.Or<Microsoft.ServiceBus.Messaging.MessagingCommunicationException>()
.Or<Microsoft.ServiceBus.Messaging.MessageLockLostException>()
.WaitAndRetryForever(
sleepDurationProvider: attempt => TimeSpan.FromMilliseconds(500), // Wait 500ms between each try.
onRetry: (exception, calculatedWaitDuration, context) => // Capture some info for logging!
{
ReportDuplicatesOrMissing("Policy.OnRetry", batchNumbers);
// This is your new exception handler!
// Tell the user what they've won!
Console.WriteLine("Log, then retry: " + exception.Message, ConsoleColor.Yellow);
retries++;
});
int i = 0;
int receiveBatchSize = 1; /* i have this in a custom settings class, hard coded here */
while (true)
{
i++;
ReportDuplicatesOrMissing("while.true", batchNumbers);
try
{
// Retry the following call according to the policy - 15 times.
pol.Execute(() =>
{
IEnumerable<BrokeredMessage> messages = null;
if (receiveBatchSize <= 1)
{
// Receive the message from the queue
BrokeredMessage receivedMessage = qc.Receive();
List<BrokeredMessage> listMsgs = new List<BrokeredMessage>();
listMsgs.Add(receivedMessage);
messages = listMsgs as IEnumerable<BrokeredMessage>;
}
else
{
messages = qc.ReceiveBatch(receiveBatchSize);
int count = messages.Count();
if (count > 0)
{
Console.WriteLine("ReceiveBatch.Count='{0}'", count);
}
}
foreach (BrokeredMessage receivedMessage in messages)
{
if (receivedMessage != null)
{
MyPayLoadObject concreteMsgObject = receivedMessage.GetBody<MyPayLoadObject>();
receivedMessage.Complete();
Console.WriteLine(@"Message received: {0}", concreteMsgObject);
}
else
{
Console.WriteLine(@"No new messages in the queue");
Thread.Sleep(1000);
}
Thread.Sleep(100);
}
});
}
catch (Exception e)
{
Console.WriteLine("Request " + i + " eventually failed with: " + e.Message, ConsoleColor.Red);
eventualFailures++;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统线程;
使用Microsoft.ServiceBus;
使用Microsoft.ServiceBus.Messaging;
使用Polly;
private static void保留对队列进行排序(QueueClient qc)
{
int重试次数=0;
int eventualFailures=0;
Policy pol=Policy.Handle(ex=>null!=ex)
.或(ex=>null!=ex)
.或()
.或()
.等待并重试(
sleepDurationProvider:trument=>TimeSpan.FromMillicles(500),//每次尝试之间等待500毫秒。
onRetry:(异常,calculatedWaitDuration,context)=>//捕获一些日志信息!
{
ReportDuplicatesOrMissing(“Policy.OnRetry”,BatchNumber);
//这是您的新异常处理程序!
//告诉用户他们赢了什么!
WriteLine(“记录,然后重试:+异常。消息,控制台颜色。黄色”);
重试++;
});
int i=0;
int receiveBatchSize=1;/*我在一个自定义设置类中有它,硬编码在这里*/
while(true)
{
i++;
ReportDuplicatesOrMissing(“while.true”,batchNumber);
尝试
{
//根据策略重试以下呼叫-15次。
pol.Execute(()=>
{
IEnumerable messages=null;
如果(接收批次大小0)
{
WriteLine(“ReceiveBatch.Count='{0}',Count);
}
}
foreach(消息中的BrokeredMessage receivedMessage)
{
if(receivedMessage!=null)
{
MyPayLoadObject concreteMsgObject=receivedMessage.GetBody();
receivedMessage.Complete();
WriteLine(@“收到的消息:{0}”,concreteMsgObject);
}
其他的
{
Console.WriteLine(@“队列中没有新消息”);
睡眠(1000);
}
睡眠(100);
}
});
}