2010-09-17 36 views

回答

1

以下是在Azure Table Whitepaper中提供的代碼,但我不確定這是否給予smark的回覆有任何價值。

/* 
     From Azure table whitepaper 

     When an exception occurs, you can extract the sequence number (highlighted above) of the command that caused the transaction to fail as follows: 

try 
{ 
    // ... save changes 
} 
catch (InvalidOperationException e) 
{ 
    DataServiceClientException dsce = e.InnerException as DataServiceClientException; 
    int? commandIndex; 
    string errorMessage; 

    ParseErrorDetails(dsce, out commandIndex, out errorMessage); 
} 


      */ 

-

void ParseErrorDetails(DataServiceClientException e, out string errorCode, out int? commandIndex, out string errorMessage) 
    { 

     GetErrorInformation(e.Message, out errorCode, out errorMessage); 

     commandIndex = null; 
     int indexOfSeparator = errorMessage.IndexOf(':'); 
     if (indexOfSeparator > 0) 
     { 
      int temp; 
      if (Int32.TryParse(errorMessage.Substring(0, indexOfSeparator), out temp)) 
      { 
       commandIndex = temp; 
       errorMessage = errorMessage.Substring(indexOfSeparator + 1); 
      } 
     } 
    } 

    void GetErrorInformation( string xmlErrorMessage, out string errorCode, out string message) 
    { 
     message = null; 
     errorCode = null; 

     XName xnErrorCode = XName.Get("code", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"); 
     XName xnMessage = XName.Get ("message", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"); 

     using (StringReader reader = new StringReader(xmlErrorMessage)) 
     { 
      XDocument xDocument = null; 
      try 
      { 
       xDocument = XDocument.Load(reader); 
      } 
      catch (XmlException) 
      { 
       // The XML could not be parsed. This could happen either because the connection 
       // could not be made to the server, or if the response did not contain the 
       // error details (for example, if the response status code was neither a failure 
       // nor a success, but a 3XX code such as NotModified. 
       return; 
      } 

      XElement errorCodeElement = xDocument.Descendants(xnErrorCode).FirstOrDefault(); 

      if (errorCodeElement == null) 
      { 
       return; 
      } 

      errorCode = errorCodeElement.Value; 

      XElement messageElement = xDocument.Descendants(xnMessage).FirstOrDefault(); 

      if (messageElement != null) 
      { 
       message = messageElement.Value; 
      } 
     } 
    } 
2

你可以嘗試尋找在響應中的值,而該內部異常。這是我嘗試catch塊的一個示例:

try { 
    return query.FirstOrDefault(); 
} 
catch (System.Data.Services.Client.DataServiceQueryException ex) 
{ 
    if (ex.Response.StatusCode == (int)System.Net.HttpStatusCode.NotFound) { 
     return null; 
    } 
    throw; 
} 

顯然,這只是項目不存在錯誤,但我敢肯定,你可以通過查看list of Azure error codes這個概念擴大。

2

看到我的代碼在這裏:http://blog.smarx.com/posts/testing-existence-of-a-windows-azure-blob。該模式用於捕獲StorageClientException,然後使用.ErrorCode屬性與StorageErrorCode中的常量進行匹配。

+1

謝謝!通常與以下相關的例外情況是:context.RetryPolicy? ..時間到? ..Expect100Continue? ..UsePostTunneling? ... MergeOption? ... servicepoint.ConnectionLimit?在創建強大的應用程序時,指南將非常有用。 – LamonteCristo 2010-09-18 20:39:26

+0

似乎表將拋出「System.Data.Services.Client.DataServiceQueryException」而不是您的博客中提到的「StorageClientException」。這將改變我的處理程序實現...到什麼,我還不確定。 – LamonteCristo 2010-09-19 18:14:24

2

要處理的錯誤,同時將對象添加到您可以使用下面的代碼表:

try { 
    _context.AddObject(TableName, entityObject); 
    _context.SaveCangesWithRetries(); 
} 
catch(DataServiceRequestException ex) { 
    ex.Response.Any(r => r.StatusCode == (int)System.Net.HttpStatusCode.Conflict) 
    throw; 
} 

正如其他回答說,你可以找到TableStorage列表錯誤在:http://msdn.microsoft.com/en-us/library/dd179438.aspx

相關問題