共用方式為


HOW TO:將 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 服務用戶端不支援 Web 服務安全性,因此應該將 System.ServiceModel.BasicHttpBinding.Security 設定為 Transport

若要讓 ASP.NET Web 服務 Proxy 產生工具 (也就是 Web 服務描述語言工具 (Wsdl.exe) (本頁面可能為英文)、Web 服務探索工具 (Disco.exe) (本頁面可能為英文) 以及 Visual Studio 中的 [加入 Web 參考] 功能) 可以使用 WCF 服務的中繼資料,您應該要公開 HTTP/GET 中繼資料端點。

若要在程式碼中新增與 ASP.NET Web 服務用戶端相容的 WCF 端點

  1. 建立新的 BasicHttpBinding 執行個體。

  2. 設定繫結至 Transport 的安全性模式,以選擇性啟用此服務端點繫結的傳輸安全性。如需詳細資訊,請參閱傳輸安全性

  3. 使用您剛建立的繫結執行個體新增新的應用程式端點到您的服務主機中。如需如何在程式碼中新增服務端點的詳細資訊,請參閱 HOW TO:在程式碼中建立服務端點

  4. 啟用您的服務的 HTTP/GET 中繼資料端點。如需詳細資訊,請參閱 HOW TO:使用程式碼發行服務的中繼資料

若要在組態檔中新增與 ASP.NET Web 服務用戶端相容的 WCF 端點

  1. 建立新的 BasicHttpBinding 繫結組態。如需詳細資訊,請參閱 HOW TO:指定組態中的服務繫結

  2. 設定繫結至 Transport 的安全性模式,以選擇性啟用此服務端點繫結組態的傳輸安全性。如需詳細資訊,請參閱傳輸安全性

  3. 使用您剛建立的繫結組態設定您的服務的新應用程式端點。如需如何在組態中新增服務端點的詳細資訊,請參閱 HOW TO:在組態中建立服務端點

  4. 啟用您的服務的 HTTP/GET 中繼資料端點。如需詳細資訊,請參閱 HOW TO:使用組態檔發行服務的中繼資料

範例

下列範例程式碼示範如何在程式碼中,或是組態檔案中新增與 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>

另請參閱

工作

HOW TO:在程式碼中建立服務端點
HOW TO:使用程式碼發行服務的中繼資料
HOW TO:指定組態中的服務繫結
HOW TO:在組態中建立服務端點
HOW TO:使用組態檔發行服務的中繼資料

概念

使用中繼資料

其他資源

傳輸安全性