Udostępnij za pośrednictwem


eConnect Integration Service exception handling

The eConnect Integration Service includes the following exception classes that you can use to catch and handle eConnect errors:

  • eConnectFault
  • eConnectSqlFault

You use these classes together with the FaultException generic class from the System.ServiceModel namespace. You use eConnectFault and eConnectSqlFault to specify the exception type. The following example shows how to use eConnectFault with the FaultException class.

FaultException<eConnectFault>

The most common exception handling technique is the Try/Catch block. For example, you place a Try block around a call to the GetEntity method. You then use one or more Catch blocks to handle exceptions. You can add code to each Catch block that attempts to correct the error, reports the error to the user, or records the error in a log.

The following C# example shows a console application that uses eConnectFault, and eConnectSqlFault together with FaultException to handle eConnect exceptions. To follow this example, you use Visual Studio to add a service reference to the eConnect Integration Service. Notice that the application does not require a reference to either of the eConnect .NET assemblies.

To create the parameters for GetEntity, the sample includes two methods:

  • The ConnectionString method creates an eConnect connection string that specifies the Microsoft Dynamics GP database you want to query.

  • The SpecifyCustomer method returns an XML string. The XML represents an eConnect requestor document that specifies the customer to retrieve. Notice how you can use the method to specify the ID of the customer you want to retrieve.

    Hint: You could also use the classes in the eConnect Serialization namespace to create the XML string. For more information, see Retrieving XML documents with GetEntity.

Finally, notice how the Dispose method is called to release the resources used by the eConnectClient.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using ExceptionHandling.eConnectIntegrationService;
namespace ExceptionHandling
{
    class Program
    {
        static void Main(string[] args)
        {
            eConnectClient client = null;
            try
            {
                // Instantiate an eConnectClient object
                client = new eConnectClient();
                // Get a requestor document for a specified customer
                string customer = client.GetEntity(
                    ConnectionString("localhost", "TWO"),
                    SpecifyCustomer("AARONFIT0001"));
                // Show customer XML document in the console window
                Console.WriteLine(customer);
                Console.WriteLine("\n\nTo continue, press any key");
                Console.ReadKey(false);
            }
            catch (FaultException<eConnectFault> eFault)
            {
                Console.WriteLine(eFault.ToString());
                Console.WriteLine("\n\nTo continue, press any key");
                Console.ReadKey(false);
            }
            catch (FaultException<eConnectSqlFault> sqlFault)
            {
                Console.WriteLine(sqlFault.ToString());
                Console.WriteLine("\n\nTo continue, press any key");
                Console.ReadKey(false);
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
                Console.WriteLine("\n\nTo continue, press any key");
                Console.ReadKey(false);
            }
            finally
            {
                if (client != null)
                {
                    client.Dispose();
                }
            }
        }
        // Return a string that represents a transaction requestor XML
        // document for the specified customer
        static string SpecifyCustomer(string custID)
        {
            return String.Format(@"<?xml version=""1.0"" ?>
                <eConnect
                xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
                xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
                <RQeConnectOutType><eConnectProcessInfo xsi:nil=""true"" />
                <taRequesterTrxDisabler_Items xsi:nil=""true"" />
                <eConnectOut><DOCTYPE>Customer</DOCTYPE>
                <OUTPUTTYPE>1</OUTPUTTYPE><INDEX1TO>{0}</INDEX1TO>
                <INDEX1FROM>{1}</INDEX1FROM><FORLIST>1</FORLIST>
                </eConnectOut></RQeConnectOutType></eConnect>",
                custID, custID);
        }
        // Return a string that represents an eConnect connection string
        // to the specified server and database
        static string ConnectionString(string dataSource, string catalog)
        {
            return String.Format(@"data source={0}; initial catalog={1};
                integrated security=SSPI; persist security info=False;
                packet size=4096", dataSource, catalog);
        }
    }
}