Home .NET Exceptions through WCF

Exceptions through WCF

by admin

Let’s talk about sending exceptions through WCF service.
To begin with let’s imagine the situation – we have a method RegisterUser, which will register the user accordingly.In case of some error in some data it throws a CustomException.
Registration code looks like this

/// <summary>
/// Registers the user.
/// </summary>
/// <param name="username"> The username.</param>
/// <param name="password"> The password.</param>
/// <param name="email"> The email.</param>
public void RegisterUser( string username, string password, string email)
{
if ( /*checking username*/ )
throw new InvalidUsernameException();
if ( /*checking password*/ )
throw new InvalidPasswordException();
if ( /*checking email*/ )
throw new InvalidEmailException();

} * This source code was highlighted with Source Code Highlighter

And this is how to use it :

/// <summary>
/// Handles the Click event of the btnRegister control.
/// </summary>
/// <param name="sender"> The source of the event.</param>
/// <param name="e"> The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void btnRegister_Click( object sender, EventArgs e)
{
try
{
RegisterUser(txtUsernamt.Text, txtPassword.Text, txtEmail.Text);
}
catch (InvalidUsernameExceptionusernameException)
{
//letting the user know that there was an error related to Username
}
catch (InvalidPasswordException passwordException)
{
//letting the user know that there was an error related to the Password
}
catch (InvalidEmailException emailException)
{
// let the user know that there was an error related to Email
}
* This source code was highlighted with Source Code Highlighter

Everything works fine, the programmer knows how to handle the error, the user knows where the error is.
Now let’s imagine that the method RegisterUser is in our WCF service.
Here’s what it says MSDN Regarding exceptions in WCF:

In all managed applications, processing errors are represented by Exception objects. In SOAP-based applications such as WCF applications, service methods communicate processing error information using SOAP fault messages. SOAP faults are message types that are included in the metadata for a service operation and therefore create a fault contract that clients can use to make their operation more robust or interactive. In addition, because SOAP faults are expressed to clients in XML form, it is a highly interoperable type system that clients on any SOAP platform can use, increasing the reach of your WCF application.

This means that SOAP fault messages are used to send an exception and they have to be handled a bit differently.
I.e. we can throwonly FaultException
But it’s not all that boring, FaultException has a generic overload FaultException<T> which we will use.
Let’s create special classes for our bugs :

/// <summary>
/// indicates an error in Username
/// </summary>
[DataContract]
class InvalidUsernameFault
{
[DataMember]
public string CustomError;
public InvalidUsernameFault()
{
}
public InvalidUsernameFault( string error)
{
CustomError = error;
}
}
/// <summary>
/// indicates an error in Password
/// </summary> [DataContract]
class InvalidPasswordFault
{
[DataMember]
public string CustomError;
public InvalidPasswordFault()
{
}
public InvalidPasswordFault( string error)
{
CustomError = error;
}
}
/// <summary>
/// Indicates an error in Email
/// </summary>
[DataContract]
class InvalidEmailFault
{
[DataMember]
public string CustomError;
public InvalidEmailFault()
{
}
public InvalidEmailFault( string error)
{
CustomError = error;
}
} * This source code was highlighted with Source Code Highlighter

Now back to our WCF service.
In the interface, we have to specify the FaultContract for our method :

[OperationContract]
[FaultContract(
typeof (InvalidUsernameFault))]
[FaultContract( typeof (InvalidPasswordFault))]
[FaultContract( typeof (InvalidEmailFault))]
void RegisterUser( string username, string password, string email);
* This source code was highlighted with Source Code Highlighter

Well now we can be sure that our exceptions will reach the customer :

/// <summary>
/// Registers the user.
/// </summary>
/// <param name="username"> The username.</param>
/// <param name="password"> The password.</param>
/// <param name="email"> The email.</param>
public void RegisterUser( string username, string password, string email)
{
if ( /*checking username*/ )
throw new FaultException<InvalidUsernameFault> ( new InvalidUsernameFault());
if ( /*checking password*/ )
throw new FaultException<InvalidPasswordFault> ( new InvalidPasswordFault());
if ( /*checking email*/ )
throw new FaultException<InvalidEmailFault> ( new InvalidEmailFault());

}Or you can throwa FaultException with a detailed error description. Handy for logging, for example : /// <summary>
/// Registers the user.
/// </summary>
/// <param name="username"> The username.</param>
/// <param name="password"> The password.</param>
/// <param name="email"> The email.</param>
public void RegisterUser( string username, string password, string email)
{
if ( /*checking username*/ )
throw new FaultException<InvalidUsernameFault> ( new InvalidUsernameFault("the Medved user is already registered”));
if ( /*checking password*/ )
throw new FaultException<InvalidPasswordFault> ( new InvalidPasswordFault("password ’12345' is invalid”));
if ( /*checking email*/ )
throw new FaultException<InvalidEmailFault> ( new InvalidEmailFault("a user with the address is already registered ya@krasafcheg.ru ”));

} * This source code was highlighted with Source Code Highlighter

Handling such exceptions is also easy :

/// <summary>
/// Handles the Click event of the btnRegister control.
/// </summary>
/// <param name="sender"> The source of the event.</param>
/// <param name="e"> The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void btnRegister_Click( object sender, EventArgs e)
{
try
{

wcfclient.RegisterUser(txtUsernamt.Text, txtPassword.Text, txtEmail.Text);
}
catch (FaultException<InvalidUsernameFault> usernameException)
{
//letting the user know that there was an error related to Username
}
catch (FaultException<InvalidPasswordFault> passwordException)
{
//letting the user know that there was an error related to the Password
}
catch (FaultException<InvalidEmailFault> emailException)
{
// let the user know that there was an error related to Email
}
catch (FaultException faultEx)
{
// handles an unrecognized error, such as an authorization error or
// connection error with the server of our WCF service
} ...
}
* This source code was highlighted with Source Code Highlighter

As you can see, there is nothing complicated! Any questions? email , we will definitely find it out 😉
Hope this helps!
Crosspost

You may also like