Handling SOAP ErrorFaults and specific WCF Exceptions
Requirements
Visual Studio 2008
Target Windows CE Device
TwinCAT ADS WCF 1.1.0.1
Compact Framework 3.5 (C#) application
using System;
using System.Windows.Forms;
using TcAdsWcf_Sample11.TwinCAT.Ads.WCF;
using System.Web.Services.Protocols;
using System.Xml;
namespace TcAdsWcf_Sample11
{
public partial class Form1 : Form
{
private UnsecBasicHttpEndpoint wcfClient;
public Form1 ( )
{
InitializeComponent ( );
// Initialize the WCF client.
wcfClient = new UnsecBasicHttpEndpoint ( );
wcfClient.Url = "http://10.1.128.80:8003/TwinCAT/Ads/Wcf/TcAdsService/UnsecBasicHttp";
}
private void button1_Click ( object sender, EventArgs e )
{
try
{
// Error provocation
byte [ ] buffer = wcfClient.Read1 ( "", 801, true, "BAD_VARNAME", 2, true);
}
catch ( SoapException soapEx )
{
if ( soapEx.Code.Name.Equals ( "AdsErrorFault" ) )
{
MessageBox.Show ( "AdsErrorFault: \n" + soapEx.Message );
}
if ( soapEx.Code.Name.Equals ( "AdsErrorErrorFault" ) )
{
// Parse details of serialized AdsErrorErrorFault object.
// Create XmlNamespaceManager and add needed namespaces.
XmlNamespaceManager nsMgr = new XmlNamespaceManager ( soapEx.Detail.OwnerDocument.NameTable );
nsMgr.AddNamespace ( "type", "http://beckhoff.com/TwinCAT/Ads/Wcf/Types" );
// Get the value of the ErrorCode property.
string errCodeVal = string.Empty;
XmlNode errCodeNode = soapEx.Detail.SelectSingleNode (
"./type:AdsErrorErrorFault/type:ErrorCode", nsMgr );
errCodeVal = errCodeNode.InnerText;
MessageBox.Show ( string.Format (
"AdsErrorErrorFault: \n{0}\n\n ErrorCode: {1}",
soapEx.Message, errCodeVal ) );
}
if ( soapEx.Code.Name.Equals ( "ArgumentErrorFault" ) )
{
MessageBox.Show ( "ArgumentErrorFault: \n" + soapEx.Message );
}
if ( soapEx.Code.Name.Equals ( "UnexpectedErrorFault" ) )
{
MessageBox.Show ( "UnexpectedErrorFault: \n" + soapEx.Message );
}
}
catch ( Exception ex )
{
MessageBox.Show ( "Exception: \n" + ex.Message );
}
}
}
}