Condividi tramite


Procedura: creare un client Web Form ASP.NET

Questo argomento è specifico di una tecnologia legacy. Servizi Web XML e client di servizi Web XML devono essere creati attualmente tramite Windows Communication Foundation.

Code Example

Un Web Form ASP.NET che agisce come un client del servizio Web differisce dagli altri client del servizio Web per il modo in cui viene fatto riferimento alla classe proxy e per il modo in cui viene distribuito. Specificamente, classi pubbliche negli assembly, distribuite nella directory \Bin sotto l'applicazione Web che contiene il Web form, possono essere create da un Web form ASP.NET. Pertanto, se si crea una classe proxy client di servizio Web, la si compila in un assembly e la si posiziona nella directory \Bin, il Web Form ASP.NET può creare un'istanza della classe proxy.

Creare un client Web form per un servizio Web.

  1. Creare un proxy client per un servizio Web.

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

    Per ulteriori informazioni, vedere Creazione di un proxy del servizio Web XML.

Compilare il proxy del servizio Web in un assembly, inclusi gli assembly System.Xml.dll e System.Web.Services.dll e il proxy creati nel passaggio 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. Creare un Web Form.

    Per ulteriori informazioni sulla creazione di un Web Form, vedere ASP.NET Web Forms Pages.

  2. Creare un'istanza della classe proxy nel codice client all'interno del Web Form.

    Counter myCounter = new Counter();
    
    Dim myCounter As New Counter()
    
  3. Eseguire la chiamata al metodo della classe proxy che comunica con il metodo del servizio Web.

    UsageCount = myCounter.ServiceUsage();
    
    UsageCount = myCounter.ServiceUsage()
    
  4. Distribuire il Web Form. Distribuire l'assembly proxy del servizio Web nella directory \Bin sotto l'applicazione Web in cui viene distribuito il Web Form.

    Per ulteriori informazioni sulla distribuzione del Web Form, vedere Deploying .NET Framework Applications.

Esempio

 <%@ 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>

Vedere anche

Concetti

Compilazione di client dei servizi Web XML

Altre risorse

Creazione di client di servizi Web XML