Share via


Procedura: Eseguire una chiamata asincrona da un client di servizio Web

Nell'esempio di codice seguente viene illustrato come eseguire una chiamata asincrona a un servizio Web da un'applicazione client. In questo esempio viene illustrata una delle linee guida spiegate nell'argomento, Progettare linee guida per i servizi Web XML creati utilizzando ASP.NET.

Security note Protezione - Nota:

L'esempio include una casella di testo che accetta l'input dell'utente e rappresenta quindi una potenziale minaccia alla sicurezza. Per impostazione predefinita, le pagine Web ASP.NET verificano che l'input dell'utente non includa script o elementi HTML. Per ulteriori informazioni, vedere Script Exploits Overview.

Esempio

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<html>
   <script language="C#" >
      void EnterBtn_Click(Object Src, EventArgs E) 
      {
         MyMath.Math math = new MyMath.Math();
         // Call to Add 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 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>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net" %>
<html>
   <script language="VB" >
      Sub EnterBtn_Click(Src As Object, E As EventArgs) 
         Dim math As New MyMath.Math()
         ' Call to Add 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 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>

Vedere anche

Attività

Procedura: Implementare un client di servizio Web asincrono tramite la tecnica Wait
Procedura: Implementare un client di servizio Web asincrono tramite la tecnica di callback

Concetti

Linee guida di progrettazione per servizi Web XML creati tramite ASP.NET
Comunicazione asincrona con i servizi Web XML

Footer image

Copyright © 2007 Microsoft Corporation. Tutti i diritti riservati.