Handling SOAP ErrorFaults and specific WCF Exceptions
Requirements
Visual Studio 2010
TwinCAT ADS WCF 1.1.0.1
C# console application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TcAdsWcf_Sample09.TwinCAT.Ads.Wcf;
using System.ServiceModel;
namespace TcAdsWcf_Sample09
{
class Program
{
static void Main ( string [ ] args )
{
// Initialize simplex client with BasicHttpBinding.
TcAdsServiceSimplexClient wcfClient = new TcAdsServiceSimplexClient (
new BasicHttpBinding ( BasicHttpSecurityMode.None ),
new EndpointAddress ( "http://localhost:8003/TwinCAT/Ads/Wcf/TcAdsService/UnsecBasicHttp" )
);
// Open connection to the WCF service.
wcfClient.Open ( );
try
{
byte[] buffer = wcfClient.Read1("", 801, "BAD_VARNAME", 2);
}
catch ( FaultException fe )
{
// Will be raised if an ErrorFault was raised by a method of TwinCAT ADS WCF.
if ( fe.GetType ( ).Equals (
typeof ( FaultException<ArgumentErrorFault> ) ) )
{
FaultException<ArgumentErrorFault> feArg =
fe as FaultException<ArgumentErrorFault>;
Console.WriteLine ( "ErrorFault: ArgumentErrorFault" );
Console.WriteLine ( feArg.Message );
}
if ( fe.GetType ( ).Equals (
typeof ( FaultException<AdsErrorFault> ) ) )
{
FaultException<AdsErrorFault> feAds =
fe as FaultException<AdsErrorFault>;
Console.WriteLine ( "ErrorFault: AdsErrorFault" );
Console.WriteLine ( feAds.Message );
}
if ( fe.GetType ( ).Equals (
typeof ( FaultException<AdsErrorErrorFault> ) ) )
{
FaultException<AdsErrorErrorFault> feAdsErr =
fe as FaultException<AdsErrorErrorFault>;
Console.WriteLine ( "ErrorFault: AdsErrorErrorFault" );
Console.WriteLine ( feAdsErr.Message );
Console.WriteLine ( "ErrorCode: " + feAdsErr.Detail.ErrorCode );
}
if ( fe.GetType ( ).Equals (
typeof ( FaultException<UnexpectedErrorFault> ) ) )
{
FaultException<UnexpectedErrorFault> feUnexpected =
fe as FaultException<UnexpectedErrorFault>;
Console.WriteLine ( "ErrorFault: UnexpectedErrorFault" );
Console.WriteLine ( feUnexpected.Message );
}
}
catch ( CommunicationException ce )
{
// Will be raised if there is a communication problem with TwinCAT ADS WCF.
Console.WriteLine ( "CommunicationException: " );
Console.WriteLine ( ce.Message );
}
catch ( TimeoutException te )
{
// Will be raised if a requested timed out.
Console.WriteLine ( "TimeoutException: " );
Console.WriteLine ( te.Message );
}
catch ( Exception ex )
{
Console.WriteLine ( "Exception: " );
Console.WriteLine ( ex.Message );
}
// Close connection to TwinCAT ADS WCF.
wcfClient.Close ( );
Console.ReadKey();
}
}
}