WebClient call to a WCF Data Service is dropping the specific exception message
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 appreciat开发者_JAVA百科ed!
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.
精彩评论