以字符串形式获取 WebClient 错误
我有一个测试网络服务的诊断工具。
我希望该工具在出现问题时进行报告,因此我部署了一个合约有问题的服务来测试它。
当我浏览它时,我会看到一个页面,其中包含非常描述性的消息,例如:
一个 ExceptionDetail,可能是由 IncludeExceptionDetailInFaults=true 创建的, 其值为: System.InvalidOperationException:调用 WSDL 时引发异常 导出扩展:
System.ServiceModel.Description.DataContractSerializerOperationBehavior 合同:类型 XXX 的 DataContract 无法添加到 DataContractSet,因为类型 命名空间 XXX 中具有相同数据协定名称 XXX 的 XXX 已存在 并且合同不等同等..
我想要的是能够调用:
myErrorMsg = WebClient.DownloadString("MyBadService.svc");
并以字符串形式获取此有用的错误消息,但是我得到以下 WebException:
远程服务器返回错误:(500) 内部服务器错误。
如何获得与浏览器中收到的相同错误消息(以字符串形式返回),而不出现异常?
谢谢。
I have a diagnostic tool which tests a web service.
I want the tool to report when there are problems, so I have deployed a service with a problem with the contract to test it.
When I browse to it I get a page with a very descriptive message such as:
An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true,
whose value is:
System.InvalidOperationException: An exception was thrown in a call to a WSDL
export extension:
System.ServiceModel.Description.DataContractSerializerOperationBehavior
contract: DataContract for type XXX cannot be added to DataContractSet since type
XXX with the same data contract name XXX in namespace XXX is already present
and the contracts are not equivalent etc..
What I want is to be able to call:
myErrorMsg = WebClient.DownloadString("MyBadService.svc");
and get this useful error message as a string, however I get the following WebException:
The remote server returned an error: (500) Internal Server Error.
How can I get the same error message I received in the browser returned as a string, without getting an exception?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须捕获异常并读取响应。
You have to catch the exception and read the response.