Export (0) Print
Expand All

CommunicationException Class

Represents a communication error in either the service or client application.

Namespace: System.ServiceModel
Assembly: System.ServiceModel (in system.servicemodel.dll)

[SerializableAttribute] 
public class CommunicationException : SystemException
/** @attribute SerializableAttribute() */ 
public class CommunicationException extends SystemException
SerializableAttribute 
public class CommunicationException extends SystemException
Not applicable.

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 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 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.

NoteNote:

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.Object
   System.Exception
     System.SystemException
      System.ServiceModel.CommunicationException
         Derived Classes

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

ADD
Show:
© 2014 Microsoft