如何:配置 WCF 服务以便与 ASP.NET Web 服务客户端进行互操作

若要将 Windows Communication Foundation (WCF) 服务终结点配置为可与 ASP.NET Web 服务客户端进行互操作,请使用 System.ServiceModel.BasicHttpBinding 类型作为服务终结点的绑定类型。

您可以根据需要在该绑定上启用对 HTTPS 和传输级客户端身份验证的支持。ASP.NET Web 服务客户端不支持 MTOM 消息编码,所以应该使 System.ServiceModel.BasicHttpBinding.MessageEncoding 属性保留其默认值,即 System.ServiceModel.WSMessageEncoding.Text。ASP.Net Web 服务客户端不支持 WS-Security,所以应将 System.ServiceModel.BasicHttpBinding.Security 设置为 Transport

若要使 WCF 服务的元数据可供 ASP.NET Web 服务代理生成工具(即 Web Services Description Language Tool (Wsdl.exe)(Web 服务描述语言工具 (Wsdl.exe))、Web Services Discovery Tool (Disco.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>

另请参见

任务

如何:在代码中创建服务终结点
如何:使用代码发布服务的元数据
如何:在配置中指定服务绑定
如何:在配置中创建服务终结点
如何:使用配置文件发布服务的元数据

概念

使用元数据

其他资源

传输安全