Posts

Showing posts with the label Faults

Handling exceptions the right way in WCF (part 2)

In my previous post I talked about FaultExceptions and how they are transmitted from the server to the client. First, let's have a look at the sample service contract : [ServiceContract] public interface ITimeService { [OperationContract] [FaultContract(typeof(TimeExceptionDetails))] DateTime GetTime(); } The service implementation : public class TimeService : ITimeService { #region ITimeService Members public DateTime GetTime() { return DateTime.Now; } #endregion } And the Fault details class : public class TimeExceptionDetails { public static readonly TimeExceptionDetails Default = new TimeExceptionDetails(); [DataMember] public String Message { get; private set; } public TimeExceptionDetails() { this.Message = "There is a problem in the spacetime continuum, Marty"; } public TimeExceptionDetails(String message) { this.Message = message; } } Now we'll learn how to ease the process of creating FaultExceptions by using the IE...

Handling exceptions the right way in WCF (part 1)

Control over exceptions is very important in WCF because exceptions can contain a lot of information about the internals of your service potentially leading to security issues. WCF allows to define not only data, service or message contracts but also fault contracts. Fault contracts are used to materialize potential exceptions in the metadata of your service. To do that, you just have to declare on your service contract which exception might be raised by a specific method. Here is a simple service contract : [ServiceContract] public interface IService1 { [OperationContract] [FaultContract(typeof(SomeFault))] void DoSomething(); } Here, we are telling the WCF runtime to expose a fault contract in the service metadata. The object containing the fault detail is named SomeFault. Here is the definition of the SomeFault class : [DataContract] public class SomeFault { [DataMember] public String SomeInfo { get; set; } } As you see, SomeFault is a data contract. Its use is only t...