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;
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.
/// <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.
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.
| < | May 2012 | |||||
|---|---|---|---|---|---|---|
| S | M | T | W | T | F | S |
| 29 | 30 | 1 | 2 | 3 | 4 | 5 |
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | 31 | 1 | 2 |
Add-ins AJAX ASP.NET MVC Browsers C# Caching CodeDom Compression CORS CSS CV Data Database DependencyResolver Development Dynamic Entity Framework Error Handling Extend File Upload Forms GDI+ HTML HTML Editor HTTP Interfaces JavaScript JQuery MCE MetadataProvider MSBuild Numbers Objects Patterns Progressive Enhancement Projects Publish Regex Resources Security SEO SMTP Source Control Strings Sub-Collections TDD Templates Tools Twitter User Interface WCF Web Development WHS WMC XLinq XML
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