Partager via


Creating an ASP.NET Web Form Client

An ASP.NET Web Form that acts as an XML Web service client differs from other XML Web service clients in how the proxy class is referenced and how it is deployed. Specifically, public classes in assemblies, deployed into the \Bin directory under the Web application containing the Web Form, can be created from an ASP.NET Web Form. Therefore, if you create an XML Web service client proxy class, compile it into an assembly, and place it in the \Bin directory, the ASP.NET Web Form can create an instance of the proxy class.

To create a Web Form client for an XML Web service

  1. Create a proxy for your XML Web service.

    Wsdl https://www.contoso.com/Counter.asmx?WSDL
    [Visual Basic]
    Wsdl /language:VB https://www.contoso.com/Counter.asmx?WSDL
    

    For more information, see Creating an XML Web Service Proxy.

  2. Compile the XML Web service proxy into an assembly, including the System.XML.dll and System.Web.Services.dll assemblies and the proxy created in step 1.

    csc /out:Counter.dll /t:library /r:System.XML.dll /r:System.Web.Services.dll Counter.cs
    [Visual Basic]
    vbc /out:Counter.dll /t:library /r:System.XML.dll,System.Web.Services.dll Counter.vb
    
  3. Create a Web Form.

    For more information about creating a Web Form, see Web Forms Pages.

  4. Create an instance of the proxy class in your client code within the Web Form.

    Counter myCounter = new Counter();
    [Visual Basic]
    Dim myCounter As New Counter()
    
  5. Call the method of the proxy class that communicates with your XML Web service method.

    UsageCount = myCounter.ServiceUsage();
    [Visual Basic]
    UsageCount = myCounter.ServiceUsage()
    
  6. Deploy the Web Form. Deploy the XML Web service proxy assembly in the \Bin directory under the Web application the Web Form is deployed to.

    For more information on deploying your Web Form, see Deploying .NET Framework Applications.

    The following example demonstrates a Web Form client for the previous XML Web service.

    <%@ Page Language="C#" %>
    <asp:Label id="Label1" runat="server" />
    <script runat=server language=c#>
    
     void Page_Load(Object o, EventArgs e){
    
      int UsageCount;
      // Create an instance of the XML Web service class.
      Counter myCounter = new Counter();
      // Call the XML Web service method ServiceUsage.
      UsageCount = myCounter.ServiceUsage();
    
      Label1.BackColor = System.Drawing.Color.DarkSlateBlue;
      Label1.ForeColor = System.Drawing.Color.Gold;
      Label1.BorderStyle = System.Web.UI.WebControls.BorderStyle.Inset;
    
      // Display the results in a Label Web Form server control.
      if (UsageCount == 1)
           Label1.Text ="XML Web service has been utilized >" + UsageCount.ToString() + "< time.";
      else   
           Label1.Text= "XML Web service has been utilized >" + UsageCount.ToString() + "< times.";
    }
    </script>
    [Visual Basic]
    <%@ Page Language="VB" %>
    <asp:Label id="Label1" runat="server" />
    <script runat=server language="VB">
    
    Sub Page_Load(o As Object, e As EventArgs)
        Dim UsageCount As Integer
        ' Create an instance of the XML Web service class.
        Dim myCounter As New Counter()
        ' Call the XML Web service method ServiceUsage.
        UsageCount = myCounter.ServiceUsage()
    
        Label1.BackColor = System.Drawing.Color.DarkSlateBlue
        Label1.ForeColor = System.Drawing.Color.Gold
        Label1.BorderStyle = System.Web.UI.WebControls.BorderStyle.Inset
    
        ' Display the results in a Label Web Form server control.
        If UsageCount = 1 Then
            Label1.Text = "XML Web service has been utilized >" & UsageCount.ToString() & "< time."
        Else
            Label1.Text = "XML Web service has been utilized >" & UsageCount.ToString() & "< times."
        End If
    End Sub
    </script>
    

See Also

Building XML Web Service Clients | Creating Clients for XML Web Services