Exceptions that are thrown by a .NET Web Service are sent back to the client in the fault element of the SOAP message. The nice thing is that you don’t have to worry about populating that element yourself. Exceptions are automatically serialized to a valid SOAP format and then deserialized so that the client can handle the SoapException like any other. So in my little prototype web service client, I’m now able to handle errors that occur with validating the user input at the client or any errors that may occur while trying to process the client request at the server and I can present the user with a nice error dialog with a useful message.

Invalid user input on the client side

An exception was thrown in our UserAdmin business object on the server
public void ErrorDialog (string message)
{
MessageDialog md = new MessageDialog (this,
DialogFlags.DestroyWithParent,
MessageType.Error,
ButtonsType.Close, message);
md.Run ();
md.Destroy();
}
public void AddClicked ()
{
AddUserDialog dialog = new AddUserDialog (this);
ResponseType resp = (ResponseType) dialog.Run ();
if (resp == ResponseType.Ok)
{
string user_id = dialog.UserId;
string fullname = dialog.FullName;
string password = dialog.Password;
string mailbox_size = dialog.MailboxSize;
try
{
service.AddUser (fullname, user_id, password,
Int32.Parse (mailbox_size));
RefreshUserList ();
}
catch (FormatException e)
{
// Client Validation failed
this.ErrorDialog (e.Message +
" Mailbox Size must be an integer!");
}
catch (SoapException e)
{
// An error occured on the Server Side
this.ErrorDialog (e.Message);
}
}
dialog.Destroy ();
}
Tags: Mono









