HOW TO:建立 ASP.NET Web Form 用戶端
本主題專門說明舊有技術。 應該使用下列建立 XML Web Service 及 XML Web Service 用戶端: Windows Communication Foundation.
Code Example
當做 Web 服務用戶端的 ASP.NET Web Form,在參考 Proxy 類別及進行部署的方式上與其他 Web 服務用戶端有所不同。具體來說,您可以從 ASP.NET Web Form 建立組件中的公用類別 (此組件會部署至包含 Web Form 之 Web 應用程式下的 \Bin 目錄)。因此,如果您建立 Web 服務用戶端 Proxy 類別,再編譯成組件,然後將它放在 \Bin 目錄中,那麼 ASP.NET Web Form 就可以建立 Proxy 類別的執行個體。
若要建立 Web 服務的 Web Form 用戶端
建立 Web 服務的 Proxy。
Wsdl https://www.contoso.com/Counter.asmx?WSDL
Wsdl /language:VB https://www.contoso.com/Counter.asmx?WSDL
如需詳細資訊,請參閱建立 XML Web Service Proxy。
將 Web 服務 Proxy 編譯成組件,其中包括 System.Xml.dll 和 System.Web.Services.dll 組件以及在步驟 1 中建立的 Proxy。
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
建立 Web Form。
如需建立 Web Form 的詳細資訊,請參閱 ASP.NET Web Forms Pages。
在 Web Form 內於用戶端程式碼中建立 Proxy 類別的執行個體。
Counter myCounter = new Counter();
Dim myCounter As New Counter()
呼叫 Proxy 類別中與 Web 服務方法通訊的方法。
UsageCount = myCounter.ServiceUsage();
UsageCount = myCounter.ServiceUsage()
部署 Web Form。在部署 Web Form 所在的 Web 應用程式底下,於 \Bin 目錄中部署 Web 服務 Proxy 組件。
如需部署 Web Form 的詳細資訊,請參閱Deploying .NET Framework Applications。
範例
<%@ 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>