Ant
no notes

MyFaultException

Everyone is used to typed exceptions, using them in your libraries is good practice allowing for concrete compile time type checking and that kind of thing. This is also a good thing to do when designing your web services.

In WCF we have FaultException<TDetail> which we can use to contractually specify what kind of exceptions a method is going to throw (everyone cheers).

Here's how;

1. Create your Detail object

This can be any old thing so long as it has a DataContract and DataMembers for the data you want to pass to the client.

/// <summary>
/// <para>Thrown when the user has a number of log on attempts 
///       which exceeds the maximum allowed</para>
/// </summary>
[DataContract(
    Name = "UserLogOnAttemptsExceededFault", 
    Namespace = "http://schemas.antix.co.uk/Antix.Data.Services.Objects")]
public class UserLogOnAttemptsExceededFault {
    /// 
    /// Create a fault exception
    /// 
    public UserLogOnAttemptsExceededFault(string userIdentifier) {
       UserIdentifier = userIdentifier;
    }

    /// 
    /// User identifier
    /// 
    [DataMember(IsRequired = true)]
    public string UserIdentifier { get; set; }
}

Here I have added a constructor to ensure that I always pass an identifier for the user in question from my service - you needn't pass anything, you can just have an empty class with the DataContract on it.

2. Contract your method defined on your ServiceContract
/// <summary>
/// <para>Security Service</para>
/// </summary>
[ServiceContract]
public interface ISecurityService {

    /// <summary>
    /// <para>Log on</para>
    /// </summary>
    /// <param name="email">Registered e-mail address</param>
    /// <param name="password">Users password</param>
    /// <returns>Session Token (GUID)</returns>
    [OperationContract]
    [FaultContract(typeof(UserNotFoundFault))]
    [FaultContract(typeof(UserLockedFault))]
    [FaultContract(typeof(UserLogOnAttemptsExceededFault))]
    [FaultContract(typeof(UserPasswordFault))]
    string LogOn(string email, string password);
}
	

I have a few other faults contracted here too.

3. Create and Throw
if (user.LogOnAttempts >= Settings.Default.UserMaxLogOnAttempts)
    throw new FaultException<UserLogOnAttemptsExceededFault>(
        new UserLogOnAttemptsExceededFault(email));
		

Now in your client, you will be able to catch the generated version like so...

var client = new SecurityServiceReference.SecurityServiceClient();
const string correctEmail = "test@example.com";
const string incorrectPassword = "notRight";

for (var i = 0; i < 10+ Settings.Default.UserMaxLogOnAttempts; i++) {
    try {
        client.LogOn(correctEmail , incorrectPassword);
    } catch (FaultException
            <SecurityServiceReference.UserPasswordFault> ex) {

        Assert.IsNotNull(ex);
        Assert.IsNotNull(ex.Detail);
        Assert.AreEqual(correctEmail, ex.Detail.UserIdentifier);
        Assert.IsTrue(i < Settings.Default.UserMaxLogOnAttempts);
    } catch (FaultException
            <SecurityServiceReference.UserLogonAttemptsExceededFault> ex) {

        Assert.IsNotNull(ex);
        Assert.IsNotNull(ex.Detail);
        Assert.AreEqual(correctEmail, ex.Detail.UserIdentifier);
        Assert.IsTrue(i >= Settings.Default.UserMaxLogOnAttempts);
    } catch (Exception) {

        Assert.Fail();
    }
}
			

This code is from a test, oooo, now for some tea.

Post a Note

(required)

(required never shown)

On Twitter Follow MrAntix on Twitter

1 hours ago
TheNextWeb
Bing's search API now live on the Windows Azure Marketplace http://t.co/utX8uOuG by @alex

15/05/2012
WindowsAzure
Announcing the MEET Windows Azure Event! Streamed online June 7th. Register at http://t.co/bObzTAuL  #MEETAzure #WindowsAzure

One hour ago
commadelimited
Buy the @amazon Kindle version of mine and @cfjedimaster's @jquerymobile book for $10 today: http://t.co/PWRZ2dkd

just now
CSSDropDownMenu
Simple horizontal css drop down menu demo Windows Azure and Cloud Computing Posts for 4/16/2011+ This makes fo... http://t.co/DZdNLHxF

just now
WAPForums
UpdateMessage() method not available in SDK 1.6? http://t.co/fyORSB1T Windows #Azure