Binding (繫結) 是 WCF 通訊堆疊的核心,決定了「如何傳輸」(Transport)、「如何編碼」(Encoding)、「何種協定」(Protocol)。選對 Binding 能大幅影響效能、相容性、安全性與部署架構。本篇聚焦兩個最常用的繫結:basicHttpBinding (Web 服務相容) 與 netTcpBinding (高效能內網),並示範程式碼與設定檔兩種端點建立方式。
| 特性 | basicHttpBinding | netTcpBinding |
|---|---|---|
| 傳輸協定 | HTTP/HTTPS | TCP (Binary) |
| 編碼方式 | Text (XML) / MTOM | Binary (專屬高效二進位) |
| 相容性 | ✅ ASMX、Java、PHP、任何 WS-I Basic Profile 1.1 客戶端 | ❌ 僅限 WCF 客戶端 |
| 效能 | 較低 (XML 解析、Base64) | ✅ 高 (二進位、連線導向、連線共用) |
| 防火牆穿透 | ✅ 80/443 通常開放 | 需開放特定 Port (預設 808/客製) |
| 安全性預設 | None (可升級 Transport/Message) | Transport (Windows 驗證) |
| 適用場景 | 跨平台、對外公開 API、Web 應用整合 | 內網高效能、雙向通訊、大量資料傳輸 |
// IHello.cs
using System.ServiceModel;
namespace WCF.ServiceLib.Binding
{
[ServiceContract]
public interface IHello
{
[OperationContract]
string SayHello(string name);
}
}
// Hello.cs
namespace WCF.ServiceLib.Binding
{
public class Hello : IHello
{
public string SayHello(string name) => $"Hello: {name}";
}
}
using System;
using System.ServiceModel;
using WCF.ServiceLib.Binding;
namespace WCF.ServiceHost2.Binding
{
class Program
{
static void Main(string[] args)
{
using (var host = new ServiceHost(typeof(Hello)))
{
// 方式 1:程式碼動態新增端點 (netTcpBinding)
host.AddServiceEndpoint(typeof(IHello), new NetTcpBinding(), "net.tcp://localhost:54321/Binding/Hello");
// 方式 2:若想同時支援 basicHttpBinding,取消註解下行
// host.AddServiceEndpoint(typeof(IHello), new BasicHttpBinding(), "https://localhost:12345/Binding/Hello");
// 中繼資料端點 (供用戶端產生 Proxy)
host.AddServiceEndpoint(typeof(IMetadataExchange), new MexHttpBinding(), "https://localhost:12345/Binding/mex");
host.Open();
Console.WriteLine("服務已啟動 (net.tcp://localhost:54321/Binding/Hello)");
Console.WriteLine("按 停止...");
Console.ReadLine();
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<services>
<service name="WCF.ServiceLib.Binding.Hello" behaviorConfiguration="BindingBehavior">
<!-- netTcpBinding 端點 -->
<endpoint address="net.tcp://localhost:54321/Binding/Hello"
binding="netTcpBinding"
contract="WCF.ServiceLib.Binding.IHello" />
<!-- basicHttpBinding 端點 (取消註解即可雙協定並存) -->
<!--
<endpoint address="https://localhost:12345/Binding/Hello"
binding="basicHttpBinding"
contract="WCF.ServiceLib.Binding.IHello" />
-->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="https://localhost:12345/Binding/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="BindingBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
using System;
using System.ServiceModel;
using System.Windows.Forms;
namespace Client2.Binding
{
public partial class HelloForm : Form
{
public HelloForm() => InitializeComponent();
private void btnSayHello_Click(object sender, EventArgs e)
{
// 方式 1:ChannelFactory 程式碼建立 (無需設定檔)
var binding = new NetTcpBinding();
var address = new EndpointAddress("net.tcp://localhost:54321/Binding/Hello");
var factory = new ChannelFactory(binding, address);
var proxy = factory.CreateChannel();
try
{
MessageBox.Show(proxy.SayHello(txtName.Text));
((ICommunicationObject)proxy).Close();
}
catch (Exception ex)
{
((ICommunicationObject)proxy).Abort();
MessageBox.Show(ex.Message);
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="net.tcp://localhost:54321/Binding/Hello"
binding="netTcpBinding"
contract="Binding.IHello" />
<!-- basicHttpBinding 端點範例 -->
<!--
<endpoint address="https://localhost:12345/Binding/Hello"
binding="basicHttpBinding"
contract="Binding.IHello" />
-->
</client>
</system.serviceModel>
</configuration>
<netTcpBinding>
<binding name="OptimizedTcpBinding"
maxReceivedMessageSize="1073741824" <!-- 1 GB -->
maxBufferSize="2147483647" <!-- 緩衝區上限 -->
transferMode="Buffered" <!-- 或 Streamed -->
connectionBufferSize="8192"
listenBacklog="100"
maxConnections="200"
receiveTimeout="00:10:00"
sendTimeout="00:10:00">
<security mode="Transport"> <!-- 預設 Windows 驗證 -->
<transport clientCredentialType="Windows" />
</security>
<reliableSession enabled="false" /> <!-- 可啟用可靠會話 -->
</binding>
</netTcpBinding>
<basicHttpBinding>
<binding name="SecureHttpBinding"
maxReceivedMessageSize="1073741824"
messageEncoding="Text" <!-- 或 Mtom -->
transferMode="Buffered">
<security mode="TransportCredentialOnly"> <!-- HTTPS + 客戶端憑證 -->
<transport clientCredentialType="Basic" /> <!-- 或 Windows, Certificate -->
</security>
<readerQuotas maxArrayLength="1073741824" />
</binding>
</basicHttpBinding>
需要跨平台/對外公開 API ? ──是──→ basicHttpBinding (或 wsHttpBinding 需 WS-* 支援)
│
否
│
內網高效能/大資料/雙向通訊 ? ──是──→ netTcpBinding
│
否
│
同機器進程間通訊 ? ───────────是──→ netNamedPipeBinding (最快、最安全)
│
否
│
需離線/佇列/保證送達 ? ────────是──→ netMsmqBinding
│
否
│
預設選擇 ──────────────────────→ basicHttpBinding (相容性最廣)