Web API 帮助程序代码:CrmHttpResponseException 类
发布日期: 2017年1月
适用于: Dynamics 365 (online),Dynamics 365 (on-premises),Dynamics CRM 2016,Dynamics CRM Online
可使用 CrmHttpResponseException 类显示在 Dynamics 365 Web API 调用期间生成的 HTTP 状态错误。 此类派生自 standard .NET System.Exception 类,可与您的现有异常处理机制轻松集成。 有关详细信息,请参阅处理和引发异常。
CrmHttpResponseException 类位于 CRM SDK Web API 帮助程序库 的 Exceptions.cs 文件中。 它在其他帮助程序库类和 C# Web API 示例中广泛使用。 有关详细信息,请参阅使用 Microsoft Dynamics 365 Web API 帮助程序库 (C# 库)。
此类利用开源 Json.NET 库中的 JSON 字符串处理功能。
类成员
下表显示 CrmHttpResponseException 类的公共成员。
CrmHttpResponseException 类 属性: StackTrace – 引发了异常(如果有)时 Dynamics 365 服务器的调用堆栈中直接框架的字符串表示。
构造函数利用此类的实例,并且需要一个 HttpContent 参数和一个可选的内部异常参数。 ExtractMessageFromContent – 此静态方法从指定的 HTTP 内容参数提取错误消息。 |
使用情况
您通常在处理返回带 HTTP 响应消息的状态错误时创建和引发 CrmHttpResponseException 对象。 例如,WhoAmI Function 函数调用失败时,以下代码引发此类错误。
response = await httpClient.GetAsync("WhoAmI", HttpCompletionOption.ResponseContentRead);
if (!response.IsSuccessStatusCode)
{
throw new CrmHttpResponseException(response.Content);
}
可以捕获和处理引发的类似其他标准 .NET 异常的 CrmHttpResponseException 对象。
重要
如果您使用 HttpResponseMessage.EnsureSuccessStatusCode 方法将 HTTP 响应错误自动转换为引发的 HttpRequestException 对象,则此方法阻止使用 CrmHttpResponseException 类。 请注意,如果使用此方法,将在处理异常时不提供大量响应消息详细信息,包括状态代码。
类列表
CRM SDK Web API 帮助程序库 NuGet 包中提供此类的最新来源。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.Crm.Sdk.Samples.HelperCode
{
/// <summary>
/// Produces a populated exception from an error message in the content of an HTTP response.
/// </summary>
public class CrmHttpResponseException : System.Exception
{
#region Properties
private static string _stackTrace;
/// <summary>
/// Gets a string representation of the immediate frames on the call stack.
/// </summary>
public override string StackTrace
{
get { return _stackTrace; }
}
#endregion Properties
#region Constructors
/// <summary>
/// Initializes a new instance of the CrmHttpResponseException class.
/// </summary>
/// <param name="content">The populated HTTP content in Json format.</param>
public CrmHttpResponseException(HttpContent content)
: base(ExtractMessageFromContent(content)) { }
/// <summary>
/// Initializes a new instance of the CrmHttpResponseException class.
/// </summary>
/// <param name="content">The populated HTTP content in Json format.</param>
/// <param name="innerexception">The exception that is the cause of the current exception, or a null reference
/// if no inner exception is specified.</param>
public CrmHttpResponseException(HttpContent content, Exception innerexception)
: base(ExtractMessageFromContent(content), innerexception) { }
#endregion Constructors
#region Methods
/// <summary>
/// Extracts the CRM specific error message and stack trace from an HTTP content.
/// </summary>
/// <param name="content">The HTTP content in Json format.</param>
/// <returns>The error message.</returns>
private static string ExtractMessageFromContent(HttpContent content)
{
string message = String.Empty;
string downloadedContent = content.ReadAsStringAsync().Result;
if (content.Headers.ContentType.MediaType.Equals("text/plain"))
{
message = downloadedContent;
}
else if (content.Headers.ContentType.MediaType.Equals("application/json"))
{
JObject jcontent = (JObject)JsonConvert.DeserializeObject(downloadedContent);
IDictionary<string, JToken> d = jcontent;
// An error message is returned in the content under the 'error' key.
if (d.ContainsKey("error"))
{
JObject error = (JObject)jcontent.Property("error").Value;
message = (String)error.Property("message").Value;
}
else if (d.ContainsKey("Message"))
message = (String)jcontent.Property("Message").Value;
if (d.ContainsKey("StackTrace"))
_stackTrace = (String)jcontent.Property("StackTrace").Value;
}
else if (content.Headers.ContentType.MediaType.Equals("text/html"))
{
message = "HTML content that was returned is shown below.";
message += "\n\n" + downloadedContent;
}
else
{
message = String.Format("No handler is available for content in the {0} format.",
content.Headers.ContentType.MediaType.ToString());
}
return message;
#endregion Methods
}
}
}
另请参阅
Microsoft Dynamics 365 Web API (C#) 入门
在 Visual Studio (C#) 中启动 Dynamics 365 Web API 项目
使用 Microsoft Dynamics 365 Web API 帮助程序库 (C# 库)
Web API 帮助程序代码:Authentication 类
Web API 帮助程序代码:配置类
Microsoft Dynamics 365
© 2017 Microsoft。 保留所有权利。 版权