Share via


方法 : 構成でサービス バインディングを指定する

この例では、簡単な電卓サービス用に ICalculator コントラクトを定義し、そのサービスを CalculatorService クラスで実装し、そのエンドポイントを Web.config ファイルで構成します。このファイルでは、サービスが BasicHttpBinding を使用するように指定します。構成の代わりにコードを使用してこのサービスを構成する方法については、「方法 : コード内でサービス バインディングを指定する」を参照してください。

通常、ベスト プラクティスは、コードで命令として記述するよりも、構成でバインディングを指定して情報を明示的にアドレス指定することです。展開済みサービスのバインディングおよびアドレスは一般的に、サービスの開発中に使用されるものとは異なるので、コード内でエンドポイントを定義することは通常、実用的ではありません。一般的に、バインディング情報とアドレス情報をコードに含めないことで、変更時にアプリケーションの再コンパイルや再展開を行う必要がなくなります。

以下のすべての構成手順は、構成エディター ツール (SvcConfigEditor.exe) を使用して実行できます。

この例のソースのコピーについては、「BasicBinding」を参照してください。

サービスの構成で BasicHttpBinding が使用されるように指定するには

  1. サービスの種類にサービス コントラクトを定義します。

      <ServiceContract()> _
    Public Interface ICalculator
          <OperationContract()> _
          Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double
          <OperationContract()> _
          Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double
          <OperationContract()> _
          Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double
          <OperationContract()> _
          Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double
      End Interface
    
    [ServiceContract]
    public interface ICalculator
    {
       [OperationContract]
       double Add(double n1, double n2);
       [OperationContract]
       double Subtract(double n1, double n2);
       [OperationContract]
       double Multiply(double n1, double n2);
       [OperationContract]
       double Divide(double n1, double n2);
    }
    
  2. サービス クラスにサービス コントラクトを実装します。

    Public Class CalculatorService
        Implements ICalculator
        Public Function Add(ByVal n1 As Double, _
                            ByVal n2 As Double) As Double Implements ICalculator.Add
            Return n1 + n2
        End Function
        Public Function Subtract(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements ICalculator.Subtract
            Return n1 - n2
        End Function
        Public Function Multiply(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements ICalculator.Multiply
            Return n1 * n2
        End Function
        Public Function Divide(ByVal n1 As Double, _
                               ByVal n2 As Double) As Double Implements ICalculator.Divide
            Return n1 / n2
        End Function
    End Class
    
    public class CalculatorService : ICalculator
    {
       public double Add(double n1, double n2)
       {
          return n1 + n2;
       }
       public double Subtract(double n1, double n2)
       {
          return n1 - n2;
       }
       public double Multiply(double n1, double n2)
       {
          return n1 * n2;
       }
       public double Divide(double n1, double n2)
       {
          return n1 / n2;
       }
    } 
    
    ms733051.note(ja-jp,VS.100).gif注 :
    アドレス情報とバインディング情報は、サービスの実装内では指定されません。また、構成ファイルからこの情報を取得するためのコードを記述する必要もありません。

  3. WSHttpBinding を使用する CalculatorService のエンドポイントを構成する Web.config ファイルを作成します。

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name=" CalculatorService" >
            <endpoint 
            <-- Leave the address blank to be populated by default-->
            <--from the hosting environment,in this case IIS, so -->
            <-- the address will just be that of the IIS Virtual -->
            <--Directory.-->
                address="" 
            <--Specify the binding type -->
                binding="wsHttpBinding"
            <--Specify the binding configuration name for that -->
            <--binding type. This is optional but useful if you  -->
            <--want to modify the properties of the binding. -->
            <--The bindingConfiguration name Binding1 is defined  -->
            <--below in the bindings element.  -->
                bindingConfiguration="Binding1"
                contract="ICalculator" />
          </service>
        </services>
        <bindings>
          <wsHttpBinding>
            <binding name="Binding1">
              <-- Binding property values can be modified here. -->
              <--See the next procedure. -->
            </binding>
          </wsHttpBinding>
       </bindings>
      </system.serviceModel>
    </configuration>
    
  4. 次の行を含む Service.svc ファイルを作成し、インターネット インフォメーション サービス (IIS: Internet Information Services) 仮想ディレクトリに配置します。

    <%@ServiceHost language=c# Service="CalculatorService" %> 
    

バインディング プロパティの既定値を変更するには

  1. WSHttpBinding の既定のプロパティ値を変更するには、<wsHttpBinding> 要素内に <binding name="Binding1"> という新しいバインディング構成名を作成し、このバインド要素のバインディングの属性に新しい値を設定します。たとえば、開く操作と閉じる操作の既定のタイムアウト値を 1 分から 2 分に変更するには、構成ファイルに以下を追加します。

          <wsHttpBinding>
            <binding name="Binding1"
                     closeTimeout="00:02:00"
                     openTimeout="00:02:00">
            </binding>
          </wsHttpBinding>
    

参照

概念

サービスとクライアントを構成するためのバインディングの使用
エンドポイント アドレスの指定