我目前正在尝试使我的应用程序能够接收来自我的服务总线的异步消息。但是,有时异常是抛出的,我不知道如何让它消失,也不知道它来自于什么。
下面是一个例外:
Exception: System.NullReferenceException: Object reference not set to an instance of an object. at GetOffers.DbConnection.processEndReceive(IAsyncResult result);所以,我知道它来自我的IASync方法,但我不能理解它在哪里不能正常工作。
我的QueueClient会议:
QClient = QueueClient.CreateFromConnectionString(connectionString, queueStr,ReceiveMode.ReceiveAndDelete);下面是我的两个IASync方法:
void processEndReceive(IAsyncResult result)
{
try
{
QueueClient qc = result.AsyncState as QueueClient;
BrokeredMessage message = qc.EndReceive(result);
ForexData data = new ForexData();
var newtrade = new Trade(data.OfferId, message.Properties["login"].ToString(), message.Properties["amount"].ToString(), message.Properties["instr"].ToString(), message.Properties["type"].ToString(), data.CreationDate, message.Properties["mode"].ToString(), message.Properties["accountID"].ToString(), message.Properties["idTrade"].ToString());
//Static Variable for the WHERE (sql) in ResponseListener Line 215.
idtradetemp = message.Properties["idTrade"].ToString();
Console.WriteLine("Received message " + message.Label);
if (newtrade != null)
{
try
{
newtrades.Add(newtrade);
}
catch
{
Console.WriteLine("Une erreur est survenue lors l'ajout du message dans la liste 'newtrades'");
}
Console.WriteLine("Received Delete: " + DateTime.Now);
}
}
catch (Exception e)
{
Console.WriteLine(string.Format("Exception: {0}", e.ToString()));
TextWriter tw = new StreamWriter("logServiceBus.txt", true);
tw.WriteLine("Program terminated on " + DateTime.Now);
tw.WriteLine("------------------------------------");
tw.WriteLine(string.Format("Exception: {0}", e.ToString()));
tw.WriteLine("------------------------------------");
tw.Close();
}
}
void processEndComplete(IAsyncResult result)
{
try
{
BrokeredMessage m = result.AsyncState as BrokeredMessage;
m.EndComplete(result);
Console.WriteLine("Completed message " + m.Label);
}
catch
{
}
}基本上,每当他在ServiceBus中检测到新消息时,就会触发这些方法。问题是,当检测到新的消息时,我放了一个Console.WriteLine(“新消息”);但是,这个CW被触发了20次(或更多)。
那么两件事中的一件:什么会导致我的错误?如何才能使我的IAsync方法在此消息上只触发一次。
发布于 2014-05-22 08:45:54
使用Begin/End对可能很难正确编程。您是否尝试过更新代码以使用异步/等待模式?发布的服务以及服务器上的Service Bus 1.1的服务总线库也支持基于任务的API。有关详细信息,请参阅http://www.windows-azure.net/task-based-apis-for-service-bus/ (我是该文章的作者)。然后,您可以使用ReceiveAsync。您可能还想看看OnMessage应用程序接口,它也可以工作。(文档在这里:http://msdn.microsoft.com/en-us/library/microsoft.servicebus.messaging.queueclient.onmessage.aspx)
https://stackoverflow.com/questions/23779275
复制相似问题