开发者

Catching a WebFaultException's details in WCF Rest

开发者 https://www.devze.com 2023-01-20 11:35 出处:网络
I have a server which is throwing WebFaultException try { ... } catch (InvalidPasswordException) { throw new WebFaultException<String>

I have a server which is throwing WebFaultException

try
{
    ...
}
catch (InvalidPasswordException)
{

    throw new WebFaultException<String>
                   ("This is a bad password", 
                    HttpStatusCode.Unauthorized);
}

So far so good. However when this WebFault is caught in another C# project (the client) I don't know how to get the details.

try
{
    HttpWebRequest httpWebRequest = WebRequest.Create(uri) as HttpWebRequest;

    httpWebRequest.Method = verb;
    httpWebRequest.ContentType = "text/xml";
    httpWebRequest.ContentLength = serializedPayload.Length;

    using (StreamWriter streamOut = new StreamWriter(httpWebRequest.GetRequestStream(), Encoding.ASCII))
    {
        streamOut.Write(serializedPayload);
        s开发者_如何学编程treamOut.Close();
    }

    // error here on GetResponseStream
    using (StreamReader streamIn = new StreamReader(httpWebRequest.GetResponse().GetResponseStream()))
    {
            string strResponse = streamIn.ReadToEnd();
            streamIn.Close();
            return strResponse;
    }
}
catch (Exception e)
{
   // The exception is of type WebException with unauthorized but don't know where the details are
}

This code catches a WebException which I can't find the details for, just the unauthorized.

Thanks

UPDATE 1: When I do the request in fiddler the response body is the detail but as that exception is thrown before the response body is ever read then it isn't showing up. So the question is how do I read the response body DESPITE a non 200 being thrown.

Fiddler Raw Response:

HTTP/1.1 401 Unauthorized
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 12 Oct 2010 22:42:31 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 100
Cache-Control: private
Content-Type: application/xml; charset=utf-8
Connection: Close

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Email/Password Mismatch</string>


I'm just guessing, but I think you just need to check the .StatusCode member of the result of GetResponse() and don't try to call GetResponseStream() unless it's 200. If it's an error code (401 in your case), then the error details should be in the .Content member of GetResponse().

Something like:

var r = httpWebRequest.GetResponse();
if(r.StatusCode != 200)
{
    MessageBox.Show(r.Content); // Display the error 
}
else
{
    var streamIn = new StreamReader(r.GetResponseStream());
    string strResponse = streamIn.ReadToEnd();
    streamIn.Close();
    return strResponse;
}


WebException has a Response property that you can cast to a HttpWebResponse and then call GetResponseStream on.

On a related note, go get yourself the Microsoft.Http library from the WCF REST Starter kit, it is a way better client library that HttpWebRequest/Response. For more details, I have some blog posts here: http://www.bizcoder.com/index.php/2009/12/08/why-the-microsoft-http-library-is-awesome/


I've used this

 try
            {
              //wcf service call
            }
            catch (FaultException ex)
            {
               throw new Exception( (ex as WebFaultException<MyContractApplicationFault>).Detail.MyContractErrorMessage );                
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号