XML Web服务的基础结构是构建来符合象SOAP、XML和WSDL这样的行业标准,并且它允许其他的平台的客户端与XML Web服务互操作。只要一个客户端可以发送符合标准的SOAP消息、依据格式化的服务描述,那么这个客户端可以调用一个使用ASP.NET创建的XML Web服务(不管客户端存在于何种平台)。
XML Web服务是一个强大的技术,用于提供可通过因特网变成访问的服务。下面的建议将帮助你创建高效的XML Web服务:
XML Web服务既支持同步的又支持异步的客户端和存放XML Web服务的服务器之间的通信。在同步通信情况下,客户端发送一个对服务的请求到服务主机服务器上等待响应。这防止客户端在等待结果的时候,执行其它的操作。然而异步通信导致客户端在等待相应的时候继续处理其它的任务。客户端在可用的时候响应服务请求的结果。
[C#] <%@ Page Language="C#" %> <%@ Import Namespace="System.Net" %> <html> <script language="C#" runat="server"> void EnterBtn_Click(Object Src, EventArgs E) { MyMath.Math math = new MyMath.Math(); // Call to Add XML Web service method asynchronously // and then wait for it to complete. IAsyncResult result = math.BeginAdd(Convert.ToInt32(Num1.Text), Convert.ToInt32(Num2.Text), null, null); // Wait for asynchronous call to complete. result.AsyncWaitHandle.WaitOne(); // Complete the asynchronous call to Add XML Web service method. float total = math.EndAdd(result); // Display results in a Label control. Total.Text = "Total: " + total.ToString(); } </script> <body> <form action="MathClient.aspx" runat=server> <font face="Verdana"> Enter the two numbers you want to add and then press the Total button. <p> Number 1: <asp:textbox id="Num1" runat=server/> + Number 2: <asp:textbox id="Num2" runat=server/> = <asp:button id="Total_Button" text="Total" OnClick="EnterBtn_Click" runat=server/> <p> <asp:label id="Total" runat=server/> </font> </form> </body> </html> [Visual Basic] <%@ Page Language="VB" %> <%@ Import Namespace="System.Net" %> <html> <script language="VB" runat="server"> Sub EnterBtn_Click(Src As Object, E As EventArgs) Dim math As New MyMath.Math() ' Call to Add XML Web service method asynchronously ' and then wait for it to complete. Dim result As IAsyncResult = _ math.BeginAdd(Convert.ToInt32(Num1.Text), _ Convert.ToInt32(Num2.Text), _ Nothing, _ Nothing)
' Wait for asynchronous call to complete. result.AsyncWaitHandle.WaitOne() ' Complete the asynchronous call to Add XML Web service method. Dim addtotal As Single = math.EndAdd(result) ' Display results in a Label control. Total.Text = "Total: " & addtotal.ToString() End Sub </script> <body> <form action="MathClient.aspx" runat=server> <font face="Verdana"> Enter the two numbers you want to add and then press the Total button. <p> Number 1: <asp:textbox id="Num1" runat=server/> + Number 2: <asp:textbox id="Num2" runat=server/> = <asp:button id="Total_Button" text="Total" OnClick="EnterBtn_Click" runat=server/> <p> <asp:label id="Total" runat=server/> </font> </form> </body> </html> 想获得更多关于异步通信的信息,请参阅后面的"和XML Web服务异步地通讯"。