Exception messages are related to specific technologies. NET exception, WCF does not support the traditional method of exception handling. If the exception is handled in a traditional manner in a WCF service, the client cannot receive an exception that is thrown by the service because the exception message cannot be serialized, such as a service design:
[ServiceContract(SessionMode = SessionMode.Allowed)]
public interface IDocumentsExplorerService
{
[OperationContract]
DocumentList FetchDocuments(string homeDir);
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class DocumentsExplorerService : IDocumentsExplorerService,IDisposable
{
public DocumentList FetchDocuments(string homeDir)
{
//Some Codes
if (Directory.Exists(homeDir))
{
//Fetch documents according to homedir
}
else
{
throw new DirectoryNotFoundException(
string.Format("Directory {0} is not found.",homeDir));
}
}
public void Dispose()
{
Console.WriteLine("The service had been disposed.");
}
}
The client cannot get the exception when it invokes a service operation such as the following:
catch (DirectoryNotFoundException ex)
{
//handle the exception;
}
To remedy this flaw, WCF treats unrecognized exceptions as FaultException exception objects, so the client can catch FaultException or exception exceptions:
catch (FaultException ex)
{
//handle the exception;
}
catch (Exception ex)
{
//handle the exception;
}