CommunicationException Class
Represents a communication error in either the service or client application.
Assembly: System.ServiceModel (in System.ServiceModel.dll)
Robust client and service Windows Communication Foundation (WCF) applications handle CommunicationException objects that may be thrown during communication. There are also two CommunicationException-derived exception types (FaultException(Of TDetail) and FaultException) that clients also often expect. Therefore, in order to prevent the generic CommunicationException handler from catching these more specific exception types, catch these exceptions prior to handling CommunicationException.
FaultException(Of TDetail) objects are thrown on the client when a SOAP fault that is specified in the operation contract is received in response to a two-way operation (that is, a method with an OperationContractAttribute attribute with IsOneWay set to false).
FaultException objects are thrown when a listener receives a SOAP fault that is not expected or specified in the operation contract. This usually occurs when the application is being debugged and the service has the IncludeExceptionDetailInFaults property set to true.
Note: |
|---|
When implementing custom channels and binding elements, it is strongly recommended that your components throw only System.TimeoutException or CommunicationException-derived objects. In the case where your components throw a recoverable exception that is specific to the component, wrap that exception inside a CommunicationException object. |
For more details about designing and using the WCF fault system, see Specifying and Handling Faults in Contracts and Services.
The following code example shows a client that handles CommunicationException types. This client also handles FaultException objects because the service has IncludeExceptionDetailInFaults set to true.
using System; using System.ServiceModel; using System.ServiceModel.Channels; using Microsoft.WCF.Documentation; public class Client { public static void Main() { // Picks up configuration from the configuration file. SampleServiceClient wcfClient = new SampleServiceClient(); try { // Making calls. Console.WriteLine("Enter the greeting to send: "); string greeting = Console.ReadLine(); Console.WriteLine("The service responded: " + wcfClient.SampleMethod(greeting)); Console.WriteLine("Press ENTER to exit:"); Console.ReadLine(); } catch (TimeoutException timeProblem) { Console.WriteLine("The service operation timed out. " + timeProblem.Message); wcfClient.Abort(); Console.ReadLine(); } // Catch the contractually specified SOAP fault raised here as an exception. catch (FaultException<GreetingFault> greetingFault) { Console.WriteLine(greetingFault.Detail.Message); Console.Read(); wcfClient.Abort(); } // Catch unrecognized faults. This handler receives exceptions thrown by WCF // services when ServiceDebugBehavior.IncludeExceptionDetailInFaults // is set to true. catch (FaultException faultEx) { Console.WriteLine("An unknown exception was received. " + faultEx.Message + faultEx.StackTrace ); Console.Read(); wcfClient.Abort(); } // Standard communication fault handler. catch (CommunicationException commProblem) { Console.WriteLine("There was a communication problem. " + commProblem.Message + commProblem.StackTrace); Console.Read(); wcfClient.Abort(); } } }
System.Exception
System.SystemException
System.ServiceModel.CommunicationException
System.ServiceModel.ActionNotSupportedException
System.ServiceModel.AddressAccessDeniedException
System.ServiceModel.AddressAlreadyInUseException
System.ServiceModel.ChannelTerminatedException
System.ServiceModel.CommunicationObjectAbortedException
System.ServiceModel.CommunicationObjectFaultedException
System.ServiceModel.Dispatcher.MessageFilterException
System.ServiceModel.EndpointNotFoundException
System.ServiceModel.FaultException
System.ServiceModel.Persistence.PersistenceException
System.ServiceModel.PoisonMessageException
System.ServiceModel.ProtocolException
System.ServiceModel.Security.MessageSecurityException
System.ServiceModel.Security.SecurityAccessDeniedException
System.ServiceModel.Security.SecurityNegotiationException
System.ServiceModel.ServerTooBusyException
System.ServiceModel.ServiceActivationException
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: