webMethods Microsoft Package 9.0 | webMethods Package for Microsoft .NET Documentation | webMethods Package for Microsoft .NET Client API Programmer’s Documentation | Invoking a Service using Java Client API from Designer | Complete IData Code Sample
 
Complete IData Code Sample
The following sample shows the all of the C# code used in Generating a C# Client Code from Designer but does not attempt to depict the end-to-end actions needed to create an executable:
using System;
using webMethods.ClientAPI;
using webMethods.ClientAPI.Data;


namespace IDataClientSample
{
/// <summary>
/// Sample client demonstrating invocation of an Integration Service
/// using the standard IData pipeline mechanism
/// </summary>
class ClientSample1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// invoke the sample service using an IData pipeline
int sum = addInts( 11, 22 );
Console.WriteLine( "sum=" + sum );
}

/**
* Add two integers together by invoking the pub.math:addInts service
* using an IData pipeline
*/
public static int addInts( int num1, int num2 )
{
// the resulting sum of the 2 integers
int sum = 0;

// create our connection context with the Integration Server
Context serverContext = new Context();

// connect to the server
try
{
serverContext.connect( "localhost:5555", "Administrator", "manage" );
}
catch( Exception ex )
{
Console.WriteLine("Connection to server failed, reason=" + ex );
return 0;
}

// create the service inputs as an IData object
IData inputIData = IDataFactory.create();
// get a data cursor for our input IData
IDataCursor inputCursor = inputIData.getCursor();

// the addInts service takes 2 inputs: num1 and num2 (as strings)
IDataUtil.put( inputCursor, "num1" , num1.ToString() );
IDataUtil.put( inputCursor, "num2" , num2.ToString() );

// done with the input cursor, destroying it only cleans up the
// cursor resources, the IData object is left unaffected
inputCursor.destroy();

try
{
// Invoke the service
IData outputIData = serverContext.invoke( pub.math,
addInts, inputIData );
// get a data cursor for out output IData
IDataCursor outputCursor = outputIData.getCursor();

// the addInts service has one return variable, "value" as string
// however we can retrieve the value as an int using the IDataUtil
// helper method getInteger();
sum = IDataUtil.getInt( outputCursor, "value", 0 );

}
catch( Exception ex )
{
Console.WriteLine("The service invoke failed, reason=" + ex );
}

// disconnect from the server
serverContext.disconnect();


return sum;
}

}
}