This page shows a small sample how to create a SOAP/web service client stub in c#. In the example, we create a stub for the WSAPIReport.wsdl service.
Requirements
You need to have installed the Microsoft .Net Software Development Kit (SDK). For our example, we use the free SharpDevelop IDE but you can use the native Microsoft IDE, too.
Creating the client stub
On your Windows PC, create a client stub by means of the following command:
wsdl.exe http://www.fleettracker.de/api/1.0/WSAPIReport.wsdl
The program wsdl.exe comes with the Microsoft SDK. It is normally included in C:\Programme\Microsoft SDKs\Windows\v...\Bin.
This will create a new file called WSAPIReport.cs. This file contains all methods needed for calling the web service.
A sample program
Open your IDE and create a new 'Windows Application Project testFTAPI. The IDE will create some convenience files for you, i.e. the file Main.cs.
You need to add the additional reference System.Web.Service from the GAC to the project.
Add the file WSAPIReport.cs to your project. Then, open the file Main.cs and add a button and a multi line text box like this:
Now, double-click on the button and add the following functions to Main.cs:
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace testFTAPI { /// <summary> /// Description of MainForm. /// </summary> public partial class MainForm : Form { public MainForm() { InitializeComponent(); } void ButtonCallFTAPIClick(object sender, EventArgs e) { WSAPIReport wsAPIReport = new WSAPIReport(); wsAPIReport.Url = "https://my.fleettracker.de/api/1.0/WSAPIReport.php"; wsAPIReport.GetReportTableCompleted += new GetReportTableCompletedEventHandler(wsAPIReport_GetReportTableCompleted); GetReportTableRequestData body = new GetReportTableRequestData(); body.apiIdentifier = new APIIdentifier(); body.apiIdentifier.companyGuid = "12345"; body.apiIdentifier.clientGuid = "1234"; wsAPIReport.GetReportTableAsync(body); } void wsAPIReport_GetReportTableCompleted(object sender, GetReportTableCompletedEventArgs e) { if (e.Error != null) { this.textBoxResult.Text = e.Error.ToString(); } else { this.textBoxResult.Text = "Result: \r\n" + e.Result.result; } } } }
The method void ButtonCallFTAPIClick(object sender, EventArgs e) is the event handler for the button buttonCallFTAPI. In this method, we create a new WSAPIReport object that is defined in the WSAPIReport.cs.
We register a callback since we call the asynchronous web service method here:
wsAPIReport.GetReportTableCompleted+= new GetReportTableCompletedEventHandler(wsAPIReport_GetReportTableCompleted);
Then, we instantiate some additional objects for calling the web service. Note that the call is not completely programmed since some input parameters are skipped here.
The call
wsAPIReport.GetReportTableAsync(body);
void wsAPIReport_GetReportTableCompleted(object sender, GetReportTableCompletedEventArgs e)
Running the program
Compile and run the program. Click on the button. After some seconds, you should see the following result:
This indicates that you have successfully called the web service (however, the API identifiers are still invalid).
As a next step, check out the e.Result object. It is of type GetReportTableResponseData and contains the requested data.