[ Home | Syllabus | Course Notes | Assignments | Search]
Key is generating web service proxy using the WSDL
Wsdl.exe generates proxies using the service
WSDL = e.g.
wsdl http://www.wintellect.com/calc.asmx
Generates a CS file containing the proxy class
proxy class derives from SoapHttpClientProtocol
Can also use simpler Get or Post protocols if only simple data types
visual studio.net runs wsdl for you when you add a web reference
Also understands UDDI
using System; class MyApp { public static void Main () { CalculatorWebService calc = new CalculatorWebService (); int sum = calc.Add (2, 2); Console.WriteLine ("2 + 2 = " + sum); } }
//------------------------------------------------------------------------------ // <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])); } }
CalculatorWebService calc = new CalculatorWebService (); IAsyncResult res = calc.BeginAdd (2, 2, null, null);
. . . // do something else while you wait
//// 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); }
F1: name of the proxy class
F2 : example of attributed programming
F3: this would change to get service elsewhere
F4: Invoke is generic procedure which takes function name and parameter array
F5: also allows asynchronous access to service
F6: can callback when done - or can call EndAdd later on
F7: actually gets the results
F8: keeps track of which asynchronous call