Chapter 6. Make a SOAP client with the free .NET framework SDK

Table of Contents
6.1. Make a SOAP client in C# using the command-line
6.1.1. Consume remote procedures returning simple type
6.1.2. Consuming SOAP procedures returning complex type with the .NET framework SDK and C#
6.1.3. C# client for the Infobel phone directory web service
6.2. Make a SOAP client in VB .NET using the command-line
6.2.1. Consume remote procedures returning simple type
6.2.2. Consuming SOAP procedures returning complex type with the .NET framework SDK and VB

In this chapter, I will use the free Microsoft .NET framework SDK to make SOAP client using the command-line compilers. You can also use the free and wonderfull IDE #DEVELOP to edit and compile your code (work also for VB .NET projects!). I will begin with example code in C# .NET, followed by VB .NET. These examples will consume my JWSDP web service (if not on-line, use it as a template for your own use), and the Infobel .NET phone directory web service (up almost 24/7).

The first thing to do (if not already done) is to install the Microsoft .NET framework SDK (Software Development Kit) from MSDN. The SDK is free and allow you to compile, using the command-line, in C# .NET, C++ .NET, VB .NET and even in J# (I won't cover the use of the J# language to make a SOAP client in my doc). If you have a compliant OS and a fast internet connection, you should be able to install all the stuff easily. Note if you already have the Microsoft Visual Studio .NET installed on your machine, you do not need to install the SDK, Visual Studio .NET includes the SDK.

We will need to use the wsdl tool to generate the stub to consume web services. To run this tool, add it to your PATH environment variable (make a search on wsdl.exe to locate it). Do the same for the bin\ directory of your .NET SDK install, so it will be easy to run the compilers: csc.exe for C# and vbc.exe for VB .NET.

6.1. Make a SOAP client in C# using the command-line

We first have to run the wsdl tool to generate our proxy. Run the tool as showed below for my on-line JWSDP web service. You can also replace the url pointing to the WSDL of my web service by one pointing to another valid WSDL document.

[your_prompt]:\> wsdl /l:cs /protocol:SOAP 
http://www.pascalbotte.be/rcx-ws/rcx?WSDL

The tool will generate, for the case of my web service, the MyRcxService class (in the MyRcxService.cs C# file). We will need this file to compile our client.

6.1.1. Consume remote procedures returning simple type

Now create our client C# file and copy/paste the code listed below:

Example 6-1. Simple C# SOAP client

In this example, We will call three simple remote procedure:

  • readLS(): this method take a string as param and return a string. This is an "echo string" demo.

  • readLSpercent(): this one take an int type as param and return an int. This method is supposed to return a real-time value of the amount of light (by the use of a light sensor) in percent (if my web service is up).

  • readTemp(): and this one take an int as param and return a float, which is the temperature (°C) in my office (again, if my web service is up).


using System;

namespace soapclient
{
  class MainClass
  {
    public static void Main(string[] args)
    {
	
        Console.WriteLine("My first C# .NET command-line SOAP client!");

        // replace eventually the class name by the one
        // generated by wsdl.exe if you ran the tool
        // for another web service
        MyRcxService mrs = new MyRcxService();
	
        // Consume the readLS() remote procedure
        // use the wrapper, 
        // this is a document/literal web service
        // see [1] for rpc/encoded
	readLS rls = new readLS();
	rls.String_1 = "your message or e-mail address";
	readLSResponse rlsresp = mrs.readLS(rls);
	Console.WriteLine("response: {0}", rlsresp.result);

        // readLSpercent()
	readLSpercent rlsp = new readLSpercent();
	rlsp.int_1 = 1;
	readLSpercentResponse rlspr = mrs.readLSpercent(rlsp);
	Console.WriteLine("Light sensor: {0}", rlspr.result);

        // temperature
	readTemp rt = new readTemp();
	rt.int_1 = 1;
	readTempResponse rtr = mrs.readTemp(rt);
	Console.WriteLine("Temperature: {0}", rtr.result);

        // use eventually a Console.ReadLine() if
        // you want to execute from an IDE,
        // so you can see the results
	//Console.ReadLine();
    }
  }
}

Now it is time to compile, run the command below to compile a PE (portable executable) as a mono-file assembly:

[your_prompt]:\> csc client.cs myrcxservice.cs

You should be able to run your exe on any computer having the .NET runtime installed. In the following sections we will create soap clients handling complex types.

6.1.3. C# client for the Infobel phone directory web service

Consuming a more advanced WSDL interface with C# .NET stay a relatively simple task. See my example below, as usually with C# you can use the wsdl tool to generate your proxy.

NoteImportant note
 

The data returned by this web service are obsolete, are from a few years ago, and can only be used for demo and testing purpose.

Run the following command:

[your_prompt]:\> wsdl /l:cs /protocol:SOAP 
http://hal.kapitol.com/infobelservices/service1.asmx?WSDL

This will create the file InfobelSearchEngines.cs. See below how to use this class.

Example 6-2. Command-line C# .NET SOAP client for the infobel web service

Also, you have an example here on how to use constant enumeration: see country, service, language and CoordType.


using System;

namespace infobelcs
{
  class MainClass
  {
    public static void Main(string[] args)
    {   
        // create the proxy
	InfobelSearchEngines ise = new InfobelSearchEngines();

        // create the CQuery object: the complex parameter
        // for the remote procedure Search()
	CQuery cq = new CQuery();
	cq.login = "infobel";
	cq.password = "test";
	cq.country = Countries.aeCountryBE;
        // we use the standard search
	cq.service = Services.aeSrvStandard;
        // search for Mr. Durant in Brussels, Belgium
	cq.Name = "durant";
	cq.City = "bruxelles";
	cq.Zip = "";
        // Coordinates in decimal degrees
	cq.CoordType = CoordTypes.aeCTWGS;
        // search in english
	cq.Language = Lang.aeLangEnglish;
        // view 5 record
	cq.PageStep = 5;
        // begin with the first
	cq.Range = 0;
	cq.XCoord = 0;
	cq.YCoord = 0;

        // make the call
	CResponse cr = ise.Search(cq);
        // old message returned by the web service
	Console.WriteLine ("Message: {0}",cr.Status);
	CResult cresult = cr.Result;
	Console.WriteLine("Records found: {0}",cresult.NumRecs);

	CRecord[] crar = cresult.collRecord;

	for(int x=0; x < crar.Length; x++)
	{
	  Console.WriteLine("\n\tName: {0}" +
		            "\n\tAddress: {1}" +
			    "\n\tCity: {2}" +
			    "\n\tZip: {5}" +
			    "\n\tPhone: {3}" +
			    "\n\tFax: {4}",crar[x].Name,
			    crar[x].Address,
			    crar[x].City,
			    crar[x].Phone,
			    crar[x].Fax,
			    crar[x].Zip);
	  if(crar[x].collCategory != null)
	  {
	    CCategory[] ccatar = crar[x].collCategory;
	    for(int y=0; y < ccatar.Length; y++)
	      Console.WriteLine("\n\t\tcategory: {0} " +
		                "code: {1}",
			        ccatar[y].description,
				ccatar[y].code);
	  }
	  Console.WriteLine();
	}
      }
    }
}

To compile this application, run the following command:

[your_prompt]:\> csc client.cs infobelsearchengine.cs

As usually, the command-line compiler will create a portable executable file. So you should be able to run it on any computer with, at least, the .NET runtime installed.

Notes

[1]

No need of the wrapper classes to consume an rpc/encoded web service. You just have to call using

// the following line will return a string
mrs.readLS("your message or e-mail");