Share via


How to: Create an ASP.NET Web Form Client

This topic is specific to a legacy technology. XML Web services and XML Web service clients should now be created using Windows Communication Foundation.

Code Example

An ASP.NET Web Form that acts as a Web service client differs from other 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 a 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 a Web service

  1. Create a proxy for your Web service.

    Wsdl https://www.contoso.com/Counter.asmx?WSDL
    
    Wsdl /language:VB https://www.contoso.com/Counter.asmx?WSDL
    

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

Compile the 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
vbc /out:Counter.dll /t:library /r:System.XML.dll,System.Web.Services.dll Counter.vb
  1. Create a Web Form.

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

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

    Counter myCounter = new Counter();
    
    Dim myCounter As New Counter()
    
  3. Call the method of the proxy class that communicates with your Web service method.

    UsageCount = myCounter.ServiceUsage();
    
    UsageCount = myCounter.ServiceUsage()
    
  4. Deploy the Web Form. Deploy the 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.

Example

 <%@ 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 Web service class.
  Counter myCounter = new Counter();
  // Call the 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 ="Web service has been utilized >" + UsageCount.ToString() + "< time.";
  else   
       Label1.Text= "Web service has been utilized >" + UsageCount.ToString() + "< times.";
}
</script>
<%@ 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 Web service class.
    Dim myCounter As New Counter()
    ' Call the 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 = "Web service has been utilized >" & UsageCount.ToString() & "< time."
    Else
        Label1.Text = "Web service has been utilized >" & UsageCount.ToString() & "< times."
    End If
End Sub
</script>

See Also

Concepts

Building XML Web Service Clients

Other Resources

Creating Clients for XML Web Services