2014-05-21 86 views
0

我正在嘗試使我的應用程序能夠從我的服務總線接收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方法在此消息上觸發一次。

回答

1

使用開始/結束對可能很難正確編程。你有沒有嘗試更新你的代碼來使用異步/等待模式?所發佈服務的Service Bus庫以及服務器上的Service Bus 1.1也支持基於任務的API。有關詳細信息,請參閱http://www.windows-azure.net/task-based-apis-for-service-bus/(我是該帖子的作者)。然後您可以使用ReceiveAsync。你也可能想看看OnMessage API,它也可以工作。 (文檔在這裏:http://msdn.microsoft.com/en-us/library/microsoft.servicebus.messaging.queueclient.onmessage.aspx

+0

我會研究它。謝謝。 – Shuiei