次の方法で共有


方法 : ASP.NET Web フォーム クライアントを作成する

このトピックの対象は、レガシ テクノロジに特定されています。XML Web サービスと XML Web サービス クライアントは以下を使用して作成してください。 Windows Communication Foundation.

Code Example

Web サービス クライアントとして機能する ASP.NET Web フォームは、プロキシ クラスの参照方法と配置方法に関して、他の Web サービス クライアントと異なります。特に、アセンブリにあるパブリック クラスは、Web フォームを含む Web アプリケーションの \Bin ディレクトリに配置され、ASP.NET Web フォームから作成できます。このため、Web サービス クライアントのプロキシ クラスを作成し、アセンブリにコンパイルして \Bin ディレクトリに配置すると、ASP.NET Web フォームでは、そのプロキシ クラスのインスタンスを作成できます。

Web サービスの Web フォーム クライアントを作成するには

  1. Web サービスのプロキシを作成します。

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

    詳細については、「XML Web サービス プロキシの作成」を参照してください。

この Web サービスのプロキシをコンパイルして、System.XML.dll アセンブリ、System.Web.Services.dll アセンブリ、および手順 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. Web フォームを作成します。

    Web フォームの作成の詳細については、「ASP.NET Web Forms Pages」を参照してください。

  2. Web フォーム内のクライアント コードにプロキシ クラスのインスタンスを作成します。

    Counter myCounter = new Counter();
    
    Dim myCounter As New Counter()
    
  3. Web サービス メソッドと通信するプロキシ クラスのメソッドを呼び出します。

    UsageCount = myCounter.ServiceUsage();
    
    UsageCount = myCounter.ServiceUsage()
    
  4. Web フォームを配置します。Web フォームの配置先の Web アプリケーションの下の \Bin ディレクトリに Web サービス プロキシ アセンブリを配置します。

    Web フォームの配置の詳細については、「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>

参照

概念

XML Web サービス クライアントの作成

その他のリソース

XML Web サービスのクライアントの作成