Wednesday, February 20, 2008

Creating a .net webservice client

I was working with getting some .net client and services with WSF/PHP. So thought of providing some usefull information to anyone interested. I will go through the process of getting WSF/PHP services and clients working with .net clients and services in the next few days. I start with writing on how to create a C# web service client, provided that you have a valid wsdl file or and endpoint which provides a valid wsdl for ?wsdl option.

Step1.
In visual Studio, go to new project->C#->Windows->Console application, and give your prefered name for a client. I will use name DemoClient for my client.


















Step 2.
Now you should be able to see the project from solutions explorer. Right click on the project and select the option, add Web Reference.

























Step3.
Then In AddWebReference dialog box, provide the url to your wsdl and click on the go button. Next provide a name for the web reference. Now you should have a web reference generated and added to your project. I used the WSDL Mode sample (wsdl_11_service.php) service that comes with WSF/PHP for generating this web reference.


















The name you give for your web reference will become a namespace in your project. I gave the name PurchaseOrder for the Web Reference. So the proxy code for this web service invocation will be generated with the namespace (Purchase Order).
Now in the my client code, I import this namespace.
using DemoClient.PurchaseOrder;
The service described in WSDL will be available as C# class to the client code. The operations defined for each service in the WSDL will become methods of the class. So invoking the web service operation is as simple as creating an instance of the required service class and calling the needed operation on it.
Eg.
ShippingTradeService stp = new ShippingTradeService();
int type = stp.GetType();

Friday, February 1, 2008

Creating a custom header using an xml string

Sometimes it is necessary to send data to the service using headers. When doing so, WSF/PHP supports two ways of doing this. It accepts recursive WSHeader arrays in its constructor. Also it is possible to put an xml string directly to the WSHeader constructor.

WSHeader constructor signature is as follows.
WSHeader:__construct(array options);

options is an associative array with the following values.
"ns"=> Namespace of the header
"prefix"=> If you want to associate a profix with the namespace, this can be used to set the prefix
"name"=> Localname of the header element
"data"=> string Array of WSHeader objects
"mustUnderstand" => Bool
"role" => int

So when sending a complex header element, one can use the array of WSHeader objects but sometimes this may get quite complex.

Instead it is now possible to pass the xml string of the header element's child to the "data".
Eg
$child = <<< XML
<product-id>5</product-id>
XML;

$header = new WSHeader(array("name"=>"product", "ns"=>'http://test.org', "data"=>$child));

How ever this only supports adding a single child element to the header. So hope to extend this to pass an array as well.