.NET Client Main-Class

This section analyzes the main-class of the .NET client example step by step.

Programming a .NET Client

The typical steps you have to follow to use PAPI Web Service with JAX-WS stack are:
  • Import the required libraries.
  • Initialize the web service.
  • Configure authentication.
  • Operate with PAPI Web Service.

Import the Required Libraries

This example uses classes from System.Web.Services.Protocols and System.Net packages. You have to import these classes to be able to use them in your code. The following code imports the classes from these packages that are used in this example.

using System.Web.Services.Protocols;
using System.Net;
            
The following code imports the classes that the client needs to perform authentication.
using Microsoft.Web.Services3.Security.Tokens;
using Microsoft.Web.Services3.Design;
               

You also have to import the automatically generated stubs. In the example project these classes were generated in the package PAPI_WS2_Sample.papiws.

using PAPI_WS2_Sample.papiws;
                

Initialize the Web Service

The following code instantiates a web service proxy. This proxy will provide access to the operations exposed by PAPI Web Service.
papiws.PapiWebServiceWse proxy = new papiws.PapiWebServiceWse();
                

Configure Authentication

Before invoking any operation over the web service the client has to authenticate. This example uses plain text Username Token Profile authentication. The authentication mechanism has to match the one you define in WSE 3 policy settings.

The following code creates a username token and sets it as the client credentials. Then it it sets the client security policy by passing the id of this policy as an argument, to the method SetPolicy().
UsernameToken token = new UsernameToken("test", "test", PasswordOption.SendPlainText);
proxy.SetClientCredential<UsernameToken>(token);
proxy.SetPolicy("ALBPM_Policy");
                

Operate with PAPI Web Service

The following code retrieves a list of available processes by invoking the method processesGetIds over a PapiWebServiceWse object. It then iterates over them using those ids to obtain the instances for each process by invoking the method processGetInstances() over the PapiWebServiceWse object.If there is a problem performing any of the requested operations, this method throws a SoapException, so you need to invoke it inside a try-catch block. It is a good practice to wrap this exception in user-defined exception. Finally it iterates over those instances invoking the method getId() and prints its result.

try {
    //...
    foreach (string processId in processIds)
    {
        Console.Out.WriteLine("\n Process: " + processId);
        instanceInfoBean[] instances = proxy.processGetInstances(processId);
        foreach (instanceInfoBean instance in instances)
        {
            Console.Out.WriteLine(" -> " + instance.id);
        }
    }

}
catch (SoapException e)
{
    OperationException oe = new OperationException(e.Message);
    throw oe;
}
                

Complete Code Example

The following class is the main class of the .NET PAPI Web Service client example.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Services.Protocols;
using System.Net;
using PAPI_WS2_Sample.papiws;

using Microsoft.Web.Services3.Security.Tokens;
using Microsoft.Web.Services3.Design;


namespace PAPI_WS2_Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            //Set a custom handler for unhandled exceptions (optional)
            AppDomain.CurrentDomain.UnhandledException += Program.UnhandledExceptionHandler;

            try
            {
                /////////////////// Initialize the web service client ///////////////////
                papiws.PapiWebServiceWse proxy = new papiws.PapiWebServiceWse();

                /////////////////// Configure authentication ///////////////////
                UsernameToken token = new UsernameToken("test", "test", PasswordOption.SendPlainText);
                proxy.SetClientCredential<UsernameToken>(token);
                proxy.SetPolicy("ALBPM_Policy");

                //set timeout and encoding
                proxy.Timeout = 60000;
                proxy.RequestEncoding = Encoding.UTF8;

                /////////////////// Operate with PAPI Web Service ///////////////////
                string[] processIds = proxy.processesGetIds(false);

                foreach (string processId in processIds)
                {
                    Console.Out.WriteLine("\n Process: " + processId);
                    instanceInfoBean[] instances = proxy.processGetInstances(processId);
                    foreach (instanceInfoBean instance in instances)
                    {
                        Console.Out.WriteLine(" -> " + instance.id);
                    }
                }

            }
            catch (SoapException e)
            {
                OperationException oe = new OperationException(e.Message);
                throw oe;
            }

        }

        static public void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
        {

            Console.Error.WriteLine("Unhandled Exception: \n" + e.ExceptionObject.ToString());
            Environment.Exit(-1);
        }
    }

    public class OperationException : Exception
    {
        public OperationException(String message) : base(message)
        {
        }
    }
}