开发者

How to determine what kind of error is returned from WCF?

Hey, I have a Silverlight app that connects to a WCF service and I'm catching errors using the event args like this:

private void GetContainersCompleted(object sender, GetContainersCompletedEventArgs e)
{
    if (e.Error != null)
    {
        // show some generic message
    }
    else
    {
        // process
    }
}

How can I determine what kind of error it is? service is down, network is unavailable, etc.

Thanks

Update:

The ones I'm interested in are connection and network exceptions, here's what I ended up doing:

private void GetContainersCompleted(object sender, GetContainersCompletedEventArgs e)
{
    if (e.Error != null)
    {
        if (e.Error.InnerException is EndpointNotFoundException ||
            e.Error.InnerExceptio开发者_开发技巧n is CommunicationException ||
            e.Error.InnerException is SecurityException)
        {
            // show connection error message
        }
        else
        {
            // show generic error message
        }
    }
    else
    {
        // process
    }
}

Any recommendations?

Thanks


You should turn on .NET WCF tracing on your server in your development environment. The WCF exceptions are frequently not helpful. This has been the only way that I have found to track down WCF issues. Especially things like the message being too large, cyclic references in data contracts, etc. I'm not sure if you can turn this on at the client level as I'm not familiar with Silverlight. Although if it can be done in the svc, it can probably be done in code also.

http://msdn.microsoft.com/en-us/library/ms733025.aspx


Try to put it in a try catch block and examine the InnerException property of the exception.

try
{
      // var something = e.Result
}
catch(Exception ex)
{
      if (ex.InnerException != null)
      {
           Log.Write(LogTypes.ERROR, "\nInner exception:\n" + ex.InnerException.StackTrace
                                       + "\nex = " + ex.InnerException.Message);
      }
}


I'm not 100% sure if this is relevant to what you're asking, but...

In an HTTP WCF service, when you throw an exception on the server, the HTTP status code of the response is 500, indicating an error. In a standard .NET client using WCF, this doesn't make any kind of difference, but in Silverlight running in the browser it does - the browser partially handles the error (that's a hand-wavy description; I don't know exactly what goes on) and what ends up getting passed to Silverlight is a generic error that doesn't have any information in it.

The way around this is to change the status code of the repsonse before it leaves the server. See here.


This CP Link has the exact post related to your query

"For WCF services, we have what's known as Faults. Simply putting it, instead of throwing an Exception in a service, we throw a special FaulException which the WCF Dispatcher handles and wraps up into our response message. This, when received by our client, gets unwrapped and thrown to the calling client method.

If we want to send extra info along with our fault, we just have to create a serializable class (using either the [Serializable] or [DataContract] attribute), and then throw the generic FaulException (where T is our serializable class) and send our class into the constructor. For the client to catch this unique exception, a [FaultContractAttribute(typeof(T))] (where T is our serializable class) must be declared above the operation contract that may throw this fault. The client proxy created by WCF will read the fault header in the returning message and try to desirialize it into a generic FaultException matching the type we defined in the contract's FaultContractAttribute."

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜