Windows Systems Programming: Spring 2004

[ Home | Syllabus | Course Notes | Assignments | Search]


Web Service Clients


CalcClient.cs

using System;

class MyApp
{
    public static void Main ()
    {
        CalculatorWebService  calc = new CalculatorWebService ();
        int sum = calc.Add (2, 2);
        Console.WriteLine ("2 + 2 = " + sum);
    }
}

Proxy class

//------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by a tool.
//     Runtime Version: 1.0.3705.0
//
//     Changes to this file may cause incorrect behavior and will be lost if 
//     the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

// 
// This source code was auto-generated by wsdl, Version=1.0.3705.0.
// 
using System.Diagnostics;
using System.Xml.Serialization; // for complex data types
using System;
using System.Web.Services.Protocols; //mostly soap also http get/post
using System.ComponentModel;
using System.Web.Services;


/// <remarks/>
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="Calculator Web ServiceSoap", Namespace="http://tempuri.org/")]
public class CalculatorWebService : System.Web.Services.Protocols.SoapHttpClientProtocol {
    
    /// <remarks/>
    public CalculatorWebService() {
        this.Url = "http://localhost/calc.asmx"; 
    }
    
    /// <remarks/>
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute(
	"http://tempuri.org/Add", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public int Add(int a, int b) {
        object[] results = this.Invoke("Add", new object[] {
                    a,
                    b});
        return ((int)(results[0]));
    }
    
    /// <remarks/>
    public System.IAsyncResult  BeginAdd(int a, int b, 
	System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("Add", new object[] {
                    a,
                    b}, callback, asyncState);
    }
	/// <remarks/>
    public int EndAdd(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((int)(results[0]));
    }
}

Asynchronous calls

CalculatorWebService calc = new CalculatorWebService ();
IAsyncResult res = calc.BeginAdd (2, 2, null, null);
   .   .   . // do something else while you wait
 int sum = calc.EndAdd (res); //// blocks if not done
//// or can check
if (res.IsCompleted) {
     int sum = calc.EndAdd (res); }
 else {     // Try again later }
//// or can be called back
AsyncCallback cb = new AsyncCallback (AddCompleted);
IAsyncResult res = calc.BeginAdd (2, 2, cb, null);
  .
  .
  .
public void AddCompleted (IAsyncResult res)
{
    int sum = calc.EndAdd (res);
}
 

 


Copyright chris wild 1999-2004.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: December 22, 2003.