定義 (W3C):支援機器間透過網路互操作的軟體系統。一組可透過網路呼叫的應用程式 API,採開放協定通訊、獨立自描述、可經 UDDI 發現。
| 標準 | 角色 |
|---|---|
| XML | 資料描述語言,自定義標籤,配合 DTD/Schema 實現自描述 |
| SOAP | 簡單物件存取協定:封裝、編碼規則、RPC 表示、綁定 (HTTP/TCP 等) |
| WSDL | Web Services 描述語言:Types、Message、portType、Binding、Services 五大元素,實現自我描述 |
| UDDI | 統一描述、發現與整合:服務註冊發現框架,基於 XML/HTTP/DNS |
適用:跨防火牆通訊、企業應用整合 (EAI)、B2B 電子商務整合、跨平台元件重用。
不適用:單機應用 (直接呼叫 Win32 API/COM 更高效)、區域網同構系統 (Remoting/COM+ 效能優越)。
部署方式:IIS 託管 (最常用,可用 ASP.NET 認證授權)、Console、Windows Service。
MySoapHeader : SoapHeader 傳遞 Token。UserValidation (可擴充資料庫驗證)。[SoapHeader("_authenticationToken")],方法中驗證 Token。MySoapHeaderValue 呼叫方法。// 自訂 SOAP Header
public class MySoapHeader : SoapHeader {
public string Token { get; set; }
}
// 驗證邏輯
public static bool IsUserLegal(string sToken) {
return string.Equals("FrankXuLei", sToken); // 實際應查資料庫
}
// 服務端
[WebService(Namespace = "https://www.cnblogs.com/frank_xl/")]
public class FrankXuWebService : WebService {
public MySoapHeader _authenticationToken;
[SoapHeader("_authenticationToken")]
[WebMethod]
public string HelloFrank() {
if (_authenticationToken != null && UserValidation.IsUserLegal(_authenticationToken.Token))
return "Hello Frank,WebMethod is called sucessfully";
throw new Exception("Authentication Failed");
}
}
// 客戶端
localhost.MySoapHeader header = new localhost.MySoapHeader { Token = "FrankXuLei" };
localhost.FrankXuWebService svc = new localhost.FrankXuWebService();
svc.MySoapHeaderValue = header;
string result = svc.HelloFrank(); // 成功回傳



本系列完整回顧 MSMQ、Remoting、Enterprise Services、Web Service 四大分散式技術,為學習 WCF 奠定堅實基礎。Web Service 雖非萬能,但在異構系統整合中不可替代。安全需分層防護:傳輸層 (SSL) + 訊息層 (WS-Security/WSE) + 應用層 (自訂驗證)。