共用方式為


作法:將 WCF 服務設為與 ASP.NET Web 服務用戶端交互操作

若要將 Windows Communication Foundation (WCF) 服務端點設定為與 ASP.NET Web 服務用戶端交互操作,請使用 System.ServiceModel.BasicHttpBinding 類型做為服務端點的繫結類型。

您可選擇啟用 HTTPS 支援及對繫結的傳輸層級用戶端驗證。 ASP.NET Web 服務用戶端不支援 MTOM 訊息編碼,因此應將 BasicHttpBinding.MessageEncoding 屬性保留為預設值,也就是 WSMessageEncoding.Text。 ASP.NET Web 服務用戶端不支援 WS-安全性,因此應該將 BasicHttpBinding.Security 設定為 Transport

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

在程式碼中新增端點

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

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

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

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

在組態檔中新增端點

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

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

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

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

範例

下列範例程式碼示範如何在程式碼中,或是設定檔中新增與 ASP.NET Web 服務用戶端相容的 WCF 端點。

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 = "http://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();
    }
}

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 = "http://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
<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>

另請參閱