获取错误详细的WCF REST [英] Getting error detail from WCF REST

查看:142
本文介绍了获取错误详细的WCF REST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

当遇到错误时REST服务将返回一个HTTP 400错误的请求包含JSON序列信息的响应主体。

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

如果我执行使用招,Javascript或直接请求从C#当错误发生时,我可以很容易地访问响应主体。

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

不过,我使用的是WCF 的ChannelFactory 6个相当复杂的接口。抛出这个代理的例外,始终是一个的ProtocolException ,没有任何有用的信息。

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?

更新

我意识到有不同的方式来做到这一点使用的.Net的负载,并且还有其他的方法来获得错误响应。他们需要了解的,但不回答这个问题。

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.

我们所使用的REST服务将改变,当他们做复杂的界面得到更新。使用的ChannelFactory 新接口意味着我们将获得编译时(而非运行时)异常,使这些一个更容易维护和更新code

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.

有没有办法使用WCF频道时,得到的回应身体的错误HTTP状态?

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

推荐答案

不要使用的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.

例如。

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

这篇关于获取错误详细的WCF REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆