Share via


Comment : procéder à un appel asynchrone d'un client de service Web

Cette rubrique est spécifique à une technologie existante. Les services Web XML et les clients du service Web XML doivent à présent être créés à l'aide de Windows Communication Foundation.

L'exemple de code suivant montre comment appeler un service Web de manière asynchrone à partir d'une application cliente. Cet exemple illustre l'une des indications expliquée dans la rubrique Règles de conception pour les services Web XML créés à l'aide d'ASP.NET.

fwb442hw.security(fr-fr,VS.100).gif Sécurité Remarque :
Cet exemple comprend une zone de texte qui accepte une entrée d'utilisateur, ce qui constitue une menace potentielle pour la sécurité. Par défaut, les pages Web ASP.NET vérifient que l'entrée d'utilisateur n'inclut pas de script ou d'éléments HTML. Pour plus d'informations, consultez Script Exploits Overview.

Exemple

<%@ 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 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" runat="server">
      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>

Voir aussi

Tâches

Comment : implémenter un client de service Web asynchrone à l'aide de la technique d'attente
Comment : implémenter un client de service Web asynchrone à l'aide de la technique de rappel

Concepts

Règles de conception pour les services Web XML créés à l'aide d'ASP.NET
Communications asynchrones avec les services Web XML