Skip to main content
Home Forums Silverlight Programming Programming with .NET - General WCF error handling anyone
9 replies. Latest Post by xsdf on August 14, 2008.
(0)
Tim Favour
Member
93 points
69 Posts
06-23-2008 7:23 AM |
Hi,
I've understood that the SOAP error handling mechanism with fault contracts doesn't work in beta 2 and possibly will not work in the RTM because of browser limitations.
Does anybody have a pattern of handling WCF errorrs in Silverlight without hacking too heavily ?
Tim
SteveWong
Contributor
6343 points
1,281 Posts
06-23-2008 8:18 AM |
Please try to refer to sladapter's post here
http://silverlight.net/forums/p/18852/64012.aspx#64012
if it still cant help, you can ask here with more details
06-23-2008 9:26 AM |
I think sladapters post is for a slightly different case where there is a 404 error but the cause is unclear.
My problem is that when I rise an custom error in the service it ends up as a 404 in my client no matter what I try to do. Creating a FaultContract for the error type does not work and I got the impression it might not work in the final release so I am looking for a clean solution to propagate server/service errors to a Silverlight client.
hwsoderlund
411 points
116 Posts
06-23-2008 12:10 PM |
I ended up rolling my own custom error object that I pass to the client as an out parameter. It means I have to do some extra coding, but I decided it was worth it, even if it might require some refactoring when (if?) proper soap exceptions can be caught on the client side.
sladapter
All-Star
17439 points
3,172 Posts
06-23-2008 12:34 PM |
You can read this thread and I posted how I handle exception in WCF and pass it to Silverlight:
http://silverlight.net/forums/p/17944/60019.aspx#60019
06-23-2008 2:32 PM |
hwsoderlund: I ended up rolling my own custom error object that I pass to the client as an out parameter. It means I have to do some extra coding, but I decided it was worth it, even if it might require some refactoring when (if?) proper soap exceptions can be caught on the client side.
Sounds like a pretty nice solution, I didn't know out parameters were allowed in service calls. Do you have any example of your solution ?
06-24-2008 3:01 AM |
The code is somewhat modified for clarity.
The error object:
[DataContract]
public class GLSErrorObject
{
[DataMember]
public string ErrorID { get; set; }
public string ErrorMessage { get; set; }
public DateTime ErrorTime { get; set; }
}
On the server side:
public bool Login(string userName, string password, out GLSErrorObject GLSError)
GLSError = null;
try
bool success = AuthenticateUser(userName, password);
return success;
catch (Exception ex)
GLSError = new GLSErrorObject();
GLSError.ErrorMessage = ex.Message;
GLSError.ErrorTime = DateTime.Now;
//etc...
return false;
And on the client side the callback method will look something like this:
void AuthenticationClient_LoginCompleted(object sender, LoginCompletedEventArgs e)
if (e.Error != null)
throw new Exception(e.Error.Message);
return;
else if (e.GLSError != null)
//Handle our custom error
throw new Exception(e.GLSError.ErrorMessage);
else
//All is well
/Henrik
06-24-2008 3:57 PM |
Hi Henrik and thanks for the example code, I guess I will implement something similar.
Btw, interesting how one can access out parameters as part of the EventArgs.
jslill
12 points
17 Posts
07-10-2008 5:54 PM |
I implemented a slightly different approach by creating a generic class to wrap a fault message string along with the actual webservice operation result. This ends up being pretty clean due to a bit of relection magic on the Silverlight client side. Here's what this would look like on the service side:
Here’s what the Silverlight client side call to the generated WCF proxy would look like:
void Div() { MyServiceClient client = new MyServiceClient(); client.DivCompleted += new EventHandler<DivCompletedEventArgs>(OnDivCompleted); client.DivAsync(10,20);}void OnDivCompleted(object sender,DivCompletedEventArgs args) { double result; try { result = GenericResult.GetOrThrow(args.Result); } catch (ServiceException e) { // Handle exceptions thrown by the service } catch (Exception e) { // Handle exceptions thrown by Silverlight or WCF }}
Note that these classes are designed to compile in Silverlight as well as normal .NET projects.
xsdf
135 points
219 Posts
08-14-2008 5:01 AM |
Good solution .