对 WCF 数据服务的 WebClient 调用正在删除特定的异常消息
我有一个 WCF 数据服务,其上有一个通过 WebClient 调用的 ServiceOperation(ServiceOperation 返回非实体数据,因此我必须解析来自 WebClient 的 XML 结果)。进行调用的代码如下所示(我已从代码中删除了一些敏感信息):
WebClient wc = new WebClient();
string requestXMLresult = wc.DownloadString(new Uri(string.Format("http://localhost:49370/DataService.svc/ServiceOperationName?<params, etc>")));
XDocument xdoc = XDocument.Parse(requestXMLresult);
// <etc>
在(例如)身份验证期间,我的服务可以抛出这样的 DataServiceException:
throw new DataServiceException(401, "The specified user did not have permissions to any resources.");
问题是,在客户端,当该异常时回来后我丢失了有关异常的额外文本。我得到的实际异常是“(401)未经授权”,就是这样。我检查了InnerException、异常消息等,似乎丢失了。
有没有办法通过 WebClient 连接保留该消息?我不确定这是否是 WCF 数据服务问题,或者是跨 WebClient 连接抛出异常的一般问题(我怀疑是后者,但我对此还很陌生)。
非常感谢任何帮助!
I have a WCF Data Service with a ServiceOperation on it that is being called via WebClient (the ServiceOperation returns non-entity data, so I have to parse the XML result from the WebClient). The code to make the call looks like this (I've removed some sensitive info from the code):
WebClient wc = new WebClient();
string requestXMLresult = wc.DownloadString(new Uri(string.Format("http://localhost:49370/DataService.svc/ServiceOperationName?<params, etc>")));
XDocument xdoc = XDocument.Parse(requestXMLresult);
// <etc>
During (for example) authentication, my service can throw a DataServiceException like this:
throw new DataServiceException(401, "The specified user did not have permissions to any resources.");
The problem is, on the client end, when that exception comes back I lose the extra text on the exception. The actual exception I get says "(401) Unauthorized", and that's it. I have checked the InnerException, exception messages, etc, and it seems to be lost.
Is there a way to preserve that message across a WebClient connection? I am not sure if this is a WCF Data Service problem, or a problem in general with exceptions thrown across WebClient connections (I suspect the latter, but I'm pretty new at this).
Any help is much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不能这样做,因为 401 是带有标准消息的标准 HTTP 错误代码。
如果您使用自己的文本返回 200,这可能会起作用,但任何 4* 或 5* 代码都只会遵循标准消息。
I don't think you can do that, because a 401 is a standard HTTP error code with a standard message.
If you return a 200 with your own text, that might work, but any of the 4* or 5* codes would just stick to the standard messages.