2014-05-21 4 views
0

내 서비스가 내 서비스 버스에서 비동기 적으로 메시지를 수신 할 수있게하려고하고 있습니다. 하지만 가끔 예외가 던지기 때문에 그걸 어떻게 사라지게 만들 수 있는지 또는 그 원인이 무엇인지 모릅니다.서비스 버스 IAsyncResult

다음은 예외입니다 :

Exception: System.NullReferenceException: Object reference not set to an instance of an  object. at GetOffers.DbConnection.processEndReceive(IAsyncResult result); 

그래서, 나는 그것이 내 IASync 방법에서 오는 알고 있지만, 나는 그것이 제대로 작동하지 않는 경우 이해할 수 아니에요.

내 QueueClient의 conf : 여기 내 두 IASync 방법

QClient = QueueClient.CreateFromConnectionString(connectionString, queueStr,ReceiveMode.ReceiveAndDelete); 

:

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 ("New Message"); 새 메시지가 감지되었지만이 CW는 2 번 (또는 그 이상) 트리거됩니다.

그래서 두 가지 중 하나 : 무엇이 오류입니까? 그리고 어떻게하면이 메시지에 대해 한 번 트리거되도록 IAsync 메서드를 만들 수 있습니까?

답변

1

시작/끝 쌍을 사용하면 올바르게 프로그래밍하기가 어려울 수 있습니다. async/await 패턴을 사용하도록 코드를 업데이트 해 보셨습니까? 릴리스 된 서비스 및 서버의 서비스 버스 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