Handling SOAP ErrorFaults and specific WCF Exceptions
Requirements
Visual Studio 2010
Silverlight 4 Tools
TwinCAT ADS WCF 1.1.0.1
Silverlight 4 application
using System;
using System.Windows;
using System.Windows.Controls;
using TcAdsWcf_Sample10.TwinCAT.Ads.Wcf;
using System.ServiceModel;
namespace TcAdsWcf_Sample10
{
public partial class MainPage : UserControl
{
private TcAdsServiceSimplexClient wcfClient;
public MainPage ( )
{
InitializeComponent ( );
// Create the TwinCAT ADS WCF client.
wcfClient = new TcAdsServiceSimplexClient (
new BasicHttpBinding ( BasicHttpSecurityMode.None ),
new EndpointAddress ( "http://localhost:8003/TwinCAT/Ads/Wcf/TcAdsService/UnsecBasicHttp" )
);
// Register Read1Completed EventHandler for Read1Async method.
wcfClient.Read1Completed += new EventHandler<Read1CompletedEventArgs1>(wcfClient_Read1Completed);
}
private void Button_Click ( object sender, RoutedEventArgs e )
{
// Call Read1Async method with an invalid TwinCAT ADS Symbol name.
wcfClient.Read1Async ( "", 801, "BAD_VARNAME", 2 );
}
private void wcfClient_Read1Completed(object sender, Read1CompletedEventArgs1 e)
{
if ( e.Error != null )
{
// An error occured while Read1Async was in process.
if ( e.Error.GetType ( ).Equals (
typeof ( FaultException<AdsErrorFault> ) ) )
{
FaultException<AdsErrorFault> fe
= e.Error as FaultException<AdsErrorFault>;
System.Diagnostics.Debug.WriteLine ( "AdsErrorFault: " );
System.Diagnostics.Debug.WriteLine ( fe.Message );
}
if ( e.Error.GetType ( ).Equals (
typeof ( FaultException<AdsErrorErrorFault> ) ) )
{
FaultException<AdsErrorErrorFault> fe
= e.Error as FaultException<AdsErrorErrorFault>;
System.Diagnostics.Debug.WriteLine ( "AdsErrorErrorFault: " );
System.Diagnostics.Debug.WriteLine ( fe.Message );
}
if ( e.Error.GetType ( ).Equals (
typeof ( FaultException<ArgumentErrorFault> ) ) )
{
FaultException<ArgumentErrorFault> fe
= e.Error as FaultException<ArgumentErrorFault>;
System.Diagnostics.Debug.WriteLine ( "ArgumentErrorFault: " );
System.Diagnostics.Debug.WriteLine ( fe.Message );
}
if ( e.Error.GetType ( ).Equals (
typeof ( FaultException<UnexpectedErrorFault> ) ) )
{
FaultException<UnexpectedErrorFault> fe
= e.Error as FaultException<UnexpectedErrorFault>;
System.Diagnostics.Debug.WriteLine ( "UnexpectedErrorFault: " );
System.Diagnostics.Debug.WriteLine ( fe.Message );
}
if ( e.Error.GetType ( ).Equals (
typeof ( CommunicationException ) ) )
{
CommunicationException ce = e.Error as CommunicationException;
System.Diagnostics.Debug.WriteLine ( "CommunicationException: " );
System.Diagnostics.Debug.WriteLine ( ce.Message );
}
if ( e.Error.GetType ( ).Equals (
typeof ( TimeoutException ) ) )
{
TimeoutException te = e.Error as TimeoutException;
System.Diagnostics.Debug.WriteLine ( "TimeoutException: " );
System.Diagnostics.Debug.WriteLine ( te.Message );
}
if ( e.Error.GetType ( ).Equals (
typeof ( Exception ) ) )
{
System.Diagnostics.Debug.WriteLine ( "Exception: " );
System.Diagnostics.Debug.WriteLine ( e.Error.Message );
}
return;
}
// No error occured.
System.Diagnostics.Debug.WriteLine ( "Completed without error!" );
}
}
}