从 WCF REST 获取错误详细信息

发布于 2024-08-26 12:09:29 字数 579 浏览 8 评论 0原文

我有一个由 .Net WCF 客户端使用的 REST 服务。

遇到错误时,REST 服务会返回 HTTP 400 错误请求,响应正文包含 JSON 序列化详细信息。

如果我使用 Fiddler、Javascript 或直接从 C# 执行请求,我可以在发生错误时轻松访问响应正文。

但是,我使用的是带有 6 个相当复杂的接口的 WCF ChannelFactory。此代理抛出的异常始终是 ProtocolException,没有任何有用的详细信息。

当我收到此错误时,有什么方法可以获取响应正文吗?


更新

我意识到有很多不同的方法可以使用.Net 来执行此操作,并且还有其他方法可以获取错误响应。了解它们很有用,但不能回答这个问题。

我们使用的 REST 服务将会发生变化,当它们发生变化时,复杂的接口就会更新。将 ChannelFactory 与新接口一起使用意味着我们将获得编译时(而不是运行时)异常,并使这些异常更容易维护和更新代码。

使用 WCF 通道时,有什么方法可以获取错误 HTTP 状态的响应正文吗?

I have a REST service consumed by a .Net WCF client.

When an error is encountered the REST service returns an HTTP 400 Bad Request with the response body containing JSON serialised details.

If I execute the request using Fiddler, Javascript or directly from C# I can easily access the response body when an error occurs.

However, I'm using a WCF ChannelFactory with 6 quite complex interfaces. The exception thrown by this proxy is always a ProtocolException, with no useful details.

Is there any way to get the response body when I get this error?


Update

I realise that there are a load of different ways to do this using .Net and that there are other ways to get the error response. They're useful to know but don't answer this question.

The REST services we're using will change and when they do the complex interfaces get updated. Using the ChannelFactory with the new interfaces means that we'll get compile time (rather than run time) exceptions and make these a lot easier to maintain and update the code.

Is there any way to get the response body for an error HTTP status when using WCF Channels?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

懷念過去 2024-09-02 12:09:30

您可以检索异常详细信息,如下所示:

                Exception innerException = exception.InnerException;
                WebException webException = innerException as WebException;
                HttpWebResponse response = webException.Response as HttpWebResponse;
                string statusDescription = response.StatusDescription;
                HttpStatusCode statusCode = response.StatusCode;

you can retrieve the exception detail as below:

                Exception innerException = exception.InnerException;
                WebException webException = innerException as WebException;
                HttpWebResponse response = webException.Response as HttpWebResponse;
                string statusDescription = response.StatusDescription;
                HttpStatusCode statusCode = response.StatusCode;
写下不归期 2024-09-02 12:09:30

ProtocolExceptionInnerException 将是 WebException
您可以从中获取 HttpWebResponse 并调用 GetResponseStream 来读取实际的响应正文。 (请记住在阅读之前先找到流的开头)。

var webException = (WebException) protocolException.InnerException;
var response = (HttpWebResponse) webException.Response;
var responseStream = response.GetResponseStream()
responseStream.Seek(0, SeekOrigin.Begin);
var reader = new StreamReader(responseStream);
var responseContent = reader.ReadToEnd();

The InnerException of the ProtocolException will be a WebException.
You can get the HttpWebResponse from that and call GetResponseStream to read the actual response body. (Remember to seek to the beginning of the stream before reading).

var webException = (WebException) protocolException.InnerException;
var response = (HttpWebResponse) webException.Response;
var responseStream = response.GetResponseStream()
responseStream.Seek(0, SeekOrigin.Begin);
var reader = new StreamReader(responseStream);
var responseContent = reader.ReadToEnd();
终遇你 2024-09-02 12:09:30

您可以尝试从服务中抛出WebProtocolException。这样,错误详细信息应包含在 HTTP 响应的正文中。看一下这篇文章:

使用 WCF 进行有效的错误处理;休息

You could try throwing a WebProtocolException from the service. This way the error details should be included in the body of the HTTP response. Have a look at this article:

Effective Error Handling with WCF & REST

若能看破又如何 2024-09-02 12:09:30

不要使用 ChannelFactory :-) 说真的。为什么要创建 REST 接口然后使用 WCF 客户端代理。使用REST服务有什么好处?为什么不直接使用 wsHttpBinding 呢?
使用 REST 入门工具包中的 HttpClient 类,您可以发出标准 HTTP 请求,然后使用 DataContractSerializer 反序列化响应。

例如

var httpClient = new HttpClient();
var content = httpClient.Get("http://example.org/customer/45").Content;
var customer = content.ReadAsDataContract<Customer>()

Don't use ChannelFactory :-) Seriously though. Why would you create a REST interface and then use the WCF client proxy. What is the benefit of using the REST service? Why not just use wsHttpBinding?
With the HttpClient class from the REST starter kit you can make standard HTTP requests and then deserialize the response using the DataContractSerializer.

E.g.

var httpClient = new HttpClient();
var content = httpClient.Get("http://example.org/customer/45").Content;
var customer = content.ReadAsDataContract<Customer>()
哑剧 2024-09-02 12:09:30

我的两点意见是,WCF 擅长使用许多不同的绑定来公开同一个类。与 C# 通信时,请使用擅长处理异常信息的 SOAP 绑定。如果必须使用 REST 样式绑定,则可以使用简单的 WebRequest 来调用服务并使用 JSON 序列化程序来反序列化结果。这也使您可以直接访问响应代码。

My two cents is that WCF is good at exposing the same class using many different bindings. When communicating with C#, use a SOAP binding that is good at exception information. If you must use the REST style binding, you could use a simple WebRequest to call the service and use the JSON serializer to deserialize the results. This will also give you direct access to the response code.

风铃鹿 2024-09-02 12:09:30

user653761 描述的方法对我有用;访问 HttpWebResponse 对象后,我可以使用 DataContractSerializer 类,如下所示:

var serializer = new DataContractSerializer(typeof(MyDataContractType));
var deserialized = 
    (serializer.ReadObject(httpWebResponse.GetResponseStream()) as MyDataContractType);

// ...

我想如果您使用正确的序列化器,这将适用于 WCF 可以序列化的任何内容,但没有'性能 t 测试(尚未)。

The approach described by user653761 works for me; after having access to the HttpWebResponse object I can use the DataContractSerializer class like this:

var serializer = new DataContractSerializer(typeof(MyDataContractType));
var deserialized = 
    (serializer.ReadObject(httpWebResponse.GetResponseStream()) as MyDataContractType);

// ...

I guess this would work for anything that WCF can serialize if you use the right serializer, didn't test for performance (yet).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文