方法 : WCF サービスおよび ASP.NET Web サービス クライアントを相互運用するために構成する

ASP.NET Web サービス クライアントと相互運用できるように Windows Communication Foundation (WCF) サービス エンドポイントを構成するには、サービス エンドポイントのバインディングの種類として System.ServiceModel.BasicHttpBinding を使用します。

必要に応じて、HTTPS およびトランスポート レベルのクライアント認証のサポートをこのバインディングで有効にできます。ASP.NET Web サービス クライアントでは MTOM メッセージ エンコーディングがサポートされていないため、System.ServiceModel.BasicHttpBinding.MessageEncoding プロパティは、既定値である System.ServiceModel.WSMessageEncoding.Text のままにしておきます。また、ASP.Net Web サービス クライアントでは WS-Security がサポートされていないため、System.ServiceModel.BasicHttpBinding.SecurityTransport に設定する必要があります。

WCF サービスのメタデータを ASP.NET Web サービス プロキシ生成ツール (つまり Web サービス記述言語ツール (Wsdl.exe)Web サービス検出ツール (Disco.exe)、および Visual Studio の Web 参照の追加機能) で利用できるようにするには、HTTP/GET メタデータ エンドポイントを公開する必要があります。

コードを使用して ASP.NET Web サービス クライアントと互換性のある WCF エンドポイントを追加するには

  1. 新しい BasicHttpBinding インスタンスを作成します。

  2. 必要に応じて、バインディングのセキュリティ モードを Transport に設定して、このサービス エンドポイント バインディングのトランスポート セキュリティを有効にします。詳細については、「トランスポート セキュリティ」を参照してください。

  3. 上で作成したバインディング インスタンスを使用して、サービス ホストに新しいアプリケーション エンドポイントを追加します。コードを使用してサービス エンドポイントを追加する方法の詳細については、「方法 : コード内にサービス エンドポイントを作成する」を参照してください。

  4. サービス用に HTTP/GET メタデータのエンドポイントを有効にします。詳細については、「方法 : コードを使用してサービスのメタデータを公開する」を参照してください。

構成ファイルを使用して ASP.NET Web サービス クライアントと互換性のある WCF エンドポイントを追加するには

  1. 新しい BasicHttpBinding バインディング構成を作成します。詳細については、「方法 : 構成でサービス バインディングを指定する」を参照してください。

  2. 必要に応じて、バインディングのセキュリティ モードを Transport に設定して、このサービス エンドポイント バインディング構成のトランスポート セキュリティを有効にします。詳細については、「トランスポート セキュリティ」を参照してください。

  3. 上で作成したバインディング構成を使用して、サービスに新しいアプリケーション エンドポイントを構成します。構成ファイルを使用してサービス エンドポイントを追加する方法の詳細については、「方法 : 構成にサービス エンドポイントを作成する」を参照してください。

  4. サービス用に HTTP/GET メタデータのエンドポイントを有効にします。詳細については、「方法 : 構成ファイルを使用してサービスのメタデータを公開する」を参照してください。

ASP.NET Web サービス クライアントと互換性のある WCF エンドポイントをコードおよび構成ファイルで追加する方法を次のコード例に示します。

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.ServiceModel
Imports System.ServiceModel.Description

<ServiceContract()> _
Public Interface IEcho

    <OperationContract()> _
    Function Echo(ByVal s As String) As String

End Interface

Public Class MyService
    Implements IEcho

    Public Function Echo(ByVal s As String) As String Implements IEcho.Echo
        Return s
    End Function

End Class

Friend Class Program

    Shared Sub Main(ByVal args() As String)
        Dim baseAddress = "https://localhost:8080/wcfselfhost/"
        Dim host As New ServiceHost(GetType(MyService), _
                                    New Uri(baseAddress))

        ' Add a service endpoint using the created binding
        With host
            .AddServiceEndpoint(GetType(IEcho), _
                                New BasicHttpBinding(), _
                                "echo1")
            .Open()
            Console.WriteLine("Service listening on {0} . . .", _
                              baseAddress)
            Console.ReadLine()
            .Close()
        End With
    End Sub
End Class
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

[ServiceContract]
public interface IEcho
{
    [OperationContract]
    string Echo(string s);
}

public class MyService : IEcho
{
    public string Echo(string s)
    {
        return s;
    }
}

class Program
{
    static void Main(string[] args)
    {
        string baseAddress = "https://localhost:8080/wcfselfhost/";
        ServiceHost host = new ServiceHost(typeof(MyService), new Uri(baseAddress));

        // Create a BasicHttpBinding instance
        BasicHttpBinding binding = new BasicHttpBinding();

        // Add a service endpoint using the created binding
        host.AddServiceEndpoint(typeof(IEcho), binding, "echo1");

        host.Open();
        Console.WriteLine("Service listening on {0} . . .", baseAddress);
        Console.ReadLine();
        host.Close();
    }
}
<configuration>
  <system.serviceModel>
    <services>
      <service name="MyService" behaviorConfiguration="HttpGetMetadata">
        <endpoint address="echo2" contract="IEcho" binding="basicHttpBinding" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="HttpGetMetadata">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

参照

処理手順

方法 : コード内にサービス エンドポイントを作成する
方法 : コードを使用してサービスのメタデータを公開する
方法 : 構成でサービス バインディングを指定する
方法 : 構成にサービス エンドポイントを作成する
方法 : 構成ファイルを使用してサービスのメタデータを公開する

概念

メタデータを使用する

その他のリソース

トランスポート セキュリティ