若要將 Windows Communication Foundation (WCF) 服務端點設定為與 ASP.NET Web 服務用戶端互通,請使用 System.ServiceModel.BasicHttpBinding 類型作為服務端點的系結類型。
您可以選擇性地在繫結上啟用 HTTPS 和傳輸層級客戶端驗證的支援。 ASP.NET Web 服務用戶端不支援 MTOM 訊息編碼,因此 BasicHttpBinding.MessageEncoding 屬性應保留為預設值,也就是 WSMessageEncoding.Text。 ASP.NET Web 服務用戶端不支援 WS-Security,因此 BasicHttpBinding.Security 應設定為 Transport。
若要讓 WCF 服務的元數據可供 ASP.NET Web 服務 Proxy 產生工具使用(也就是 Web 服務描述語言工具(Wsdl.exe)、Web 服務探索工具(Disco.exe),以及 Visual Studio 中 新增 Web 參考 功能),您應該公開 HTTP/GET 元數據端點。
在程式代碼中新增端點
建立新的 BasicHttpBinding 實例
選擇性地將系結的安全性模式設定為 Transport,以啟用此服務端點系結的傳輸安全性。 如需詳細資訊,請參閱 傳輸安全性。
使用您剛才建立的系結實例,將新的應用程式端點新增至服務主機。 如需如何在程式代碼中新增服務端點的詳細資訊,請參閱 如何:在程式代碼中建立服務端點。
為您的服務啟用 HTTP/GET 元資料端點。 如需詳細資訊,請參閱 如何:使用程式代碼發佈服務的元資料。
在組態檔中新增端點
建立新的 BasicHttpBinding 系結組態。 如需詳細資訊,請參閱 如何:在設定中指定服務系結。
選擇性地啟用此服務端點系結組態的傳輸安全性,方法是將繫結的安全性模式設定為 Transport。 如需詳細資訊,請參閱 運輸安全性。
使用您剛才建立的系結組態,為您的服務設定新的應用程式端點。 如需如何在組態檔中新增服務端點的詳細資訊,請參閱 How to: Create a Service Endpoint in Configuration。
為您的服務啟用 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 {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>
另請參閱
- 如何:在程式代碼 中建立服務端點
- 如何:使用程式代碼發佈服務的元數據
- 如何在設定中指定服務繫結
- 如何:在組態 中建立服務端點
- 如何:使用組態檔發佈服務的元數據
- 傳輸安全性
- 使用元數據