我正在嘗試使我的應用程序能夠從我的服務總線接收Asyncronously消息。 但是,有時一個例外是拋出,我不知道如何使它失去或從它可能來自什麼。服務總線IAsyncResult
這裏的例外:
Exception: System.NullReferenceException: Object reference not set to an instance of an object. at GetOffers.DbConnection.processEndReceive(IAsyncResult result);
所以,我知道它來自我的IASync方法,但我無法理解它不能正常工作。
我QueueClient的conf:
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
{
}
}
Basicaly這些方法是觸發每次他在ServiceBus檢測新郵件。事情是,我已經把一個Console.WriteLine(「新消息」);當檢測到新消息時,但是,這個CW會觸發2次(或更多次)。
所以有兩件事之一: 什麼會導致我的錯誤?以及如何讓我的IAsync方法在此消息上觸發一次。
我會研究它。謝謝。 – Shuiei