2012-09-03 88 views
1

我從工作者角色向服務總線隊列發送郵件。我注意到隨機的一些消息丟失了。Azure服務總線隊列:郵件未被髮送

當我調試時,我在發送方法後設置斷點並登錄到我的Azure面板以檢查消息隊列是否增加。奇怪的是,我發現消息不會添加到隊列中。但它不是隨機的。模式是:一條消息正確添加,下一條丟失,然後再次確定,下一條丟失。

我已經實現了類似下面的重試模式,但顯然不是所有的消息都發送正確。

我的代碼是:

var baseAddress = RoleEnvironment.GetConfigurationSettingValue("namespaceAddress"); 
var issuerName = RoleEnvironment.GetConfigurationSettingValue("issuerName"); 
var issuerKey = RoleEnvironment.GetConfigurationSettingValue("issuerKey"); 
var retryStrategy = new FixedInterval(5, TimeSpan.FromSeconds(2)); 
var retryPolicy = new RetryPolicy<ServiceBusTransientErrorDetectionStrategy>(retryStrategy); 
Uri namespaceAddress = ServiceBusEnvironment.CreateServiceUri("sb", baseAddress, string.Empty); 

this.namespaceManager = new NamespaceManager(namespaceAddress, TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerKey)); 
this.messagingFactory = MessagingFactory.Create(namespaceAddress, TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerKey)); 
//namespaceManager.GetQueue("chatmessage"); 
QueueClient client = messagingFactory.CreateQueueClient("chatmessage"); 
APPService.Model.MessageReceived messageReceived = new Model.MessageReceived(); 
messageReceived.From= e.From; 
messageReceived.Op = e.Operator; 
messageReceived.Message = e.Body; 
BrokeredMessage msg = null; 

// Use a retry policy to execute the Send action in an asynchronous and reliable fashion. 
retryPolicy.ExecuteAction 
(
    (cb) => 
    { 
     // A new BrokeredMessage instance must be created each time we send it. Reusing the original BrokeredMessage instance may not 
     // work as the state of its BodyStream cannot be guaranteed to be readable from the beginning. 
     msg = new BrokeredMessage(messageReceived); 

     // Send the event asynchronously. 
     client.BeginSend(msg, cb, null); 
    }, 
    (ar) => 
    { 
     try 
     { 
      // Complete the asynchronous operation. This may throw an exception that will be handled internally by the retry policy. 
      client.EndSend(ar); 
     } 
     finally 
     { 
      // Ensure that any resources allocated by a BrokeredMessage instance are released. 
      if (msg != null) 
      { 
       msg.Dispose(); 
       msg = null; 
      } 
     } 
    }, 
    ()=>{}, 
    (ex) => 
    { 
     // Always dispose the BrokeredMessage instance even if the send operation has completed unsuccessfully. 
     if (msg != null) 
     { 
      msg.Dispose(); 
      msg = null; 
     } 

     // Always log exceptions. 
     Trace.TraceError(ex.Message); 
    } 
); 

出了什麼問題?

+0

如果我在'''await qc.SendAsync'''後面處理它們,我一直在發現消息不會發送。 – mcintyre321

回答

1

只看你的代碼,我無法注意到任何雲導致的問題。 我想門戶上的計數器並沒有實時更新,所以我不會將其視爲測試結果。

我知道我現在沒有幫助,但是如果我有疑問表達,我會建立一個端到端測試來發送和接收消息,比較messageId。

您是否已在MSDN上使用Paolo Salvatori的Service Bus Explorer?您可以使用它來測試實體。它是探索和測試服務總線的可靠工具。

+0

謝謝,我將檢查Service Bus Explorer。 –

相關問題