This section analyzes the main-class of the .NET client example step by step.
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;
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;
papiws.PapiWebServiceWse proxy = new papiws.PapiWebServiceWse();
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.
UsernameToken token = new UsernameToken("test", "test", PasswordOption.SendPlainText);
proxy.SetClientCredential<UsernameToken>(token);
proxy.SetPolicy("ALBPM_Policy");
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;
}
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)
{
}
}
}