创建简单的XML 服务
<%@ WebService Class="TemperatureService" Language="c#" %>
using System;
using System.Web.Services;
public class TemperatureService : System.Web.Services.WebService
{
[WebMethod()]
public double ToCelsius( double TF )
{
return ( 5d/9d ) * (TF - 32) ;
}
[WebMethod()]
public double ToFahrenheit( double TC,double my )
{
return ( 9d/5d ) * (TC + 32)+my;
}
}
保存为TemperatureService.asmx
通过wsdl.exe /t:cs http://localhost/asp%20skill/Chapter22/cs/TemperatureService.asmx?wsdl 编译成代理类会生成一个文件,TemperatureService.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.42
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
//
// This source code was auto-generated by wsdl, Version=2.0.50727.42.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="TemperatureServiceSoap", Namespace="http://tempuri.org/")]
public partial class TemperatureService : System.Web.Services.Protocols.SoapHttpClientProtocol {
private System.Threading.SendOrPostCallback ToCelsiusOperationCompleted;
private System.Threading.SendOrPostCallback ToFahrenheitOperationCompleted;
/// <remarks/>
public TemperatureService() {
this.Url = "http://localhost/asp%20skill/chapter22/cs/TemperatureService.asmx";
}
/// <remarks/>
public event ToCelsiusCompletedEventHandler ToCelsiusCompleted;
/// <remarks/>
public event ToFahrenheitCompletedEventHandler ToFahrenheitCompleted;
/// <remarks/>
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/ToCelsius", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public double ToCelsius(double TF) {
object[] results = this.Invoke("ToCelsius", new object[] {
TF});
return ((double)(results[0]));
}
/// <remarks/>
public System.IAsyncResult BeginToCelsius(double TF, System.AsyncCallback callback, object asyncState) {
return this.BeginInvoke("ToCelsius", new object[] {
TF}, callback, asyncState);
}
/// <remarks/>
public double EndToCelsius(System.IAsyncResult asyncResult) {
object[] results = this.EndInvoke(asyncResult);
return ((double)(results[0]));
}
/// <remarks/>
public void ToCelsiusAsync(double TF) {
this.ToCelsiusAsync(TF, null);
}
/// <remarks/>
public void ToCelsiusAsync(double TF, object userState) {
if ((this.ToCelsiusOperationCompleted == null)) {
this.ToCelsiusOperationCompleted = new System.Threading.SendOrPostCallback(this.OnToCelsiusOperationCompleted);
}
this.InvokeAsync("ToCelsius", new object[] {
TF}, this.ToCelsiusOperationCompleted, userState);
}
private void OnToCelsiusOperationCompleted(object arg) {
if ((this.ToCelsiusCompleted != null)) {
System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
this.ToCelsiusCompleted(this, new ToCelsiusCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
}
}
/// <remarks/>
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/ToFahrenheit", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public double ToFahrenheit(double TC, double my) {
object[] results = this.Invoke("ToFahrenheit", new object[] {
TC,
my});
return ((double)(results[0]));
}
/// <remarks/>
public System.IAsyncResult BeginToFahrenheit(double TC, double my, System.AsyncCallback callback, object asyncState) {
return this.BeginInvoke("ToFahrenheit", new object[] {
TC,
my}, callback, asyncState);
}
/// <remarks/>
public double EndToFahrenheit(System.IAsyncResult asyncResult) {
object[] results = this.EndInvoke(asyncResult);
return ((double)(results[0]));
}
/// <remarks/>
public void ToFahrenheitAsync(double TC, double my) {
this.ToFahrenheitAsync(TC, my, null);
}
/// <remarks/>
public void ToFahrenheitAsync(double TC, double my, object userState) {
if ((this.ToFahrenheitOperationCompleted == null)) {
this.ToFahrenheitOperationCompleted = new System.Threading.SendOrPostCallback(this.OnToFahrenheitOperationCompleted);
}
this.InvokeAsync("ToFahrenheit", new object[] {
TC,
my}, this.ToFahrenheitOperationCompleted, userState);
}
private void OnToFahrenheitOperationCompleted(object arg) {
if ((this.ToFahrenheitCompleted != null)) {
System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
this.ToFahrenheitCompleted(this, new ToFahrenheitCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
}
}
/// <remarks/>
public new void CancelAsync(object userState) {
base.CancelAsync(userState);
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42")]
public delegate void ToCelsiusCompletedEventHandler(object sender, ToCelsiusCompletedEventArgs e);
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class ToCelsiusCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
private object[] results;
internal ToCelsiusCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
base(exception, cancelled, userState) {
this.results = results;
}
/// <remarks/>
public double Result {
get {
this.RaiseExceptionIfNecessary();
return ((double)(this.results[0]));
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42")]
public delegate void ToFahrenheitCompletedEventHandler(object sender, ToFahrenheitCompletedEventArgs e);
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class ToFahrenheitCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
private object[] results;
internal ToFahrenheitCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
base(exception, cancelled, userState) {
this.results = results;
}
/// <remarks/>
public double Result {
get {
this.RaiseExceptionIfNecessary();
return ((double)(this.results[0]));
}
}
}
将TemperatureService.cs编译成dll文件,放到你的bin目录下,你就可以通过代理类来访问XML WEB
<%@ Page Language="C#" %>
<script language="C#" runat=server>
void Button_Click(Object sender , EventArgs e)
{
CompiledTemperatureService objTemp = new CompiledTemperatureService();
lblCelsius.Text = objTemp.ToCelsius(Double.Parse(txtFahrenheit.Text)).ToString("n");
}
void ChangeTheTemperature(object sender, EventArgs e)
{
TemperatureService objTs = new TemperatureService();
myLabel.Text = objTs.ToCelsius(Double.Parse(myTB.Text)).ToString("n");
}
</Script>
<html>
<head><title>ConvertTemperature.aspx</title></head>
<body>
<form Runat="Server">
Fahrenheit:
<asp:TextBox
ID="txtFahrenheit"
Runat="Server" />
<asp:Button
Text="Convert!"
OnClick="Button_Click"
Runat="Server" />
<hr>
<asp:Label
ID="lblCelsius"
Runat="Server" />
<br />
<asp:TextBox ID="myTB" runat="server" />
<asp:Button ID="myB" runat="server" OnClick="ChangeTheTemperature" />
<asp:Label ID="myLabel" runat="server" />
</form>
</body>
</html>
用HTTP-GET 调用 XML Web服务
http://localhost/asp%20skill/Chapter22/cs/TemperatureService.asmx/ToCelsius?TF=32
用HTTP-POST 调用 XML Web服务
<form method="post" action="./TemperatureService.asmx/ToCelsius">
<input name="TF" value="32"> <input type="submit" value="Convert!">
</form>