Client in .NET
From Section 7.2 of the book
This example shows how to write a .NET client for the TimeService.
client/NetClient/NetClient.cs
using System;
using TimeClient; // namespace of the generated proxy
public class NetClient {
public static void Main(string[] args) {
TimeService service = new TimeService();
Console.WriteLine("The time on the server is: ");
string time = service.GetTime();
Console.WriteLine(time);
}
}
|
The class NetClient uses the proxy TimeClient.TimeService
to call GetTime. This proxy is stored in the file TimeServiceProxy.cs
and was generated with the tool wsdl.exe using the following command:
client/NetClient/genproxy.bat
wsdl /namespace:TimeClient /out:TimeServiceProxy.cs
http://dotnet.jku.at/book/samples/7/simple/TimeService.asmx
|
The generated proxy has methods for calling the web service synchronously (GetTime())
and asynchronously (BeginGetTime() and EndGetTime()):
client/NetClient/TimeServiceProxy.cs
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version: 1.0.3705.209
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
//
// This source code was auto-generated by wsdl, Version=1.0.3705.209.
//
namespace TimeClient {
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;
///
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="TimeServiceSoap", Namespace="http://tempuri.org/")]
public class TimeService : System.Web.Services.Protocols.SoapHttpClientProtocol {
///
public TimeService() {
this.Url = "http://dotnet.jku.at/book/samples/7/simple/TimeService1.asmx";
}
///
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/GetTime", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public string GetTime() {
object[] results = this.Invoke("GetTime", new object[0]);
return ((string)(results[0]));
}
///
public System.IAsyncResult BeginGetTime(System.AsyncCallback callback, object asyncState) {
return this.BeginInvoke("GetTime", new object[0], callback, asyncState);
}
///
public string EndGetTime(System.IAsyncResult asyncResult) {
object[] results = this.EndInvoke(asyncResult);
return ((string)(results[0]));
}
}
}
|
|