从 WCF REST 获取错误详细信息
我有一个由 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以检索异常详细信息,如下所示:
you can retrieve the exception detail as below:
ProtocolException
的InnerException
将是WebException
。您可以从中获取
HttpWebResponse
并调用GetResponseStream
来读取实际的响应正文。 (请记住在阅读之前先找到流的开头)。The
InnerException
of theProtocolException
will be aWebException
.You can get the
HttpWebResponse
from that and callGetResponseStream
to read the actual response body. (Remember to seek to the beginning of the stream before reading).您可以尝试从服务中抛出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
不要使用 ChannelFactory :-) 说真的。为什么要创建 REST 接口然后使用 WCF 客户端代理。使用REST服务有什么好处?为什么不直接使用 wsHttpBinding 呢?
使用 REST 入门工具包中的 HttpClient 类,您可以发出标准 HTTP 请求,然后使用 DataContractSerializer 反序列化响应。
例如
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.
我的两点意见是,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.
user653761 描述的方法对我有用;访问
HttpWebResponse
对象后,我可以使用DataContractSerializer
类,如下所示:我想如果您使用正确的序列化器,这将适用于 WCF 可以序列化的任何内容,但没有'性能 t 测试(尚未)。
The approach described by user653761 works for me; after having access to the
HttpWebResponse
object I can use theDataContractSerializer
class like this:I guess this would work for anything that WCF can serialize if you use the right serializer, didn't test for performance (yet).