DPWS (Device Profile for Web Services) 是 Windows Rally 技術子集,針對資源受限裝置定義最小化安全 Web Service 介面集。Windows Vista 內建 WSDAPI 客戶端,支援 DPWS 裝置自動發現於網路鄰居。本文完整示範在 .NET Micro Framework v3.0 上建立 DPWS Host 與 Client,於 Windows Vista 環境執行驗證。

| 命名空間 | 說明 | 組件 |
|---|---|---|
| Dpws.Device | DPWS 裝置堆疊核心類別、屬性控制 | MFDpwsDevice.dll |
| Dpws.Device.Services | 事件來源、主機服務端點/操作/事件管理 | MFDpwsDevice.dll |
| Ws.Services | 端點服務操作管理 | MFWsStack.dll |
| Ws.Services.Faults | 錯誤處理類別 | MFWsStack.dll |
| Ws.Services.Mtom | SOAP MTOM 處理 | MFWsStack.dll |
| Ws.Services.Soap | SOAP 訊息解析 | MFWsStack.dll |
| Ws.Services.Transport | 基礎網路傳輸服務 | MFWsStack.dll |
| Ws.Services.Utilities | 除錯輸出、持續時間、UUID 驗證 | MFWsStack.dll |
| Ws.Services.WsaAddressing | WS-Addressing 標頭處理 | MFWsStack.dll |
| Ws.Services.Xml | XML 處理工具 | MFWsStack.dll |
| 命名空間 | 說明 |
|---|---|
| Dpws.Client | DPWS 客戶端建立 |
| Dpws.Client.Discovery | 查找與解析請求 |
| Dpws.Client.Eventing | 事件訂閱客製化 |
| Dpws.Client.Transport | HTTP 請求發送/接收 |
建立 ASP.NET Web Service 專案,定義 Add 方法:
[WebService(Namespace = "https://fox23.cnblogs.com/")]
public class Service1 : System.Web.Services.WebService {
[WebMethod]
public int Add(int a, int b) { return a + b; }
}
瀏覽服務取得 WSDL,另存為 AddService.wsdl。
SDK 工具目錄執行:
MfSvcUtil AddService.wsdl
產生三個檔案:
AddService.cs - 資料合約類別AddServiceHostedService.cs - 主機服務基類AddServiceClientProxy.cs - 客戶端 Proxy新增 .NET Micro Framework Console 專案,引用四個組件,加入產生的三個類別。
using System;
using Microsoft.SPOT;
using Dpws.Device;
using fox.cnblogs.com; // 產生類別命名空間
public class Program {
public static void Main() {
// 設定裝置端點 (必須 urn:uuid 格式)
Device.EndpointAddress = "urn:uuid:587066b0-440f-4652-8395-ec20cca0ec91";
// 裝置中繼資料配置
Device.ThisModel.Manufacturer = "Freesc Huang";
Device.ThisModel.ManufacturerUrl = "https://fox23.cnblogs.com/";
Device.ThisModel.ModelName = "Freesc Dpws Test Device";
Device.ThisModel.ModelNumber = "1.0";
Device.ThisModel.ModelUrl = "https://fox23.cnblogs.com/";
Device.ThisModel.PresentationUrl = "https://fox23.cnblogs.com/";
Device.ThisDevice.FriendlyName = "Service1Soap";
Device.ThisDevice.FirmwareVersion = "alpha";
Device.ThisDevice.SerialNumber = "12345678";
// 實作服務介面
public class AddServiceImplementation : IService1Soap {
public AddResponse Add(Add req) {
AddResponse response = new AddResponse();
response.AddResult = req.a + req.b;
return response;
}
}
// 註冊並啟動服務
Device.HostedServices.Add(new Service1Soap(new AddServiceImplementation()));
Device.Start();
Debug.Print("Device started!");
Thread.Sleep(Timeout.Infinite);
}
}
public class Program {
public static void Main() {
// 發現裝置
DpwsServiceDescription resolveMatch = Discover.DiscoveryClient.Resolve(
"urn:uuid:587066b0-440f-4652-8395-ec20cca0ec91");
// 取得裝置中繼資料並尋找服務端點
var deviceMetadata = Discover.MexClient.Get(deviceEndpointURI);
string serviceEndpoint = null;
if (deviceMetadata.Relationship.HostedServices != null) {
for (int i = 0; i < deviceMetadata.Relationship.HostedServices.Count; ++i) {
if (deviceMetadata.Relationship.HostedServices[i].ServiceID ==
"urn:uuid:2c7ea8f1-d812-402d-afa8-6ca9fa03a2f0") {
serviceEndpoint = deviceMetadata.Relationship.HostedServices[i]
.EndpointRefs[0].Address.AbsoluteUri;
}
}
}
// 呼叫服務
Service1SoapClientProxy soapProxy = new Service1SoapClientProxy();
soapProxy.ServiceEndpoint = serviceEndpoint;
soapProxy.IgnoreRequestFromThisIP = false;
Add request = new Add { a = 2, b = 5 };
AddResponse response = soapProxy.Add(request);
Debug.Print("mResponse.AddResult: " + response.AddResult); // 輸出 7
}
}
雙 Visual Studio 分別對模擬器偵錯,先啟 Host 再啟 Client:
HOST:
urn:uuid:587066b0-440f-4652-8395-ec20cca0ec91
Device started!
CLIENT:
Device endpoint found: https://192.168.1.101:8084/587066b0-440f-4652-8395-ec20cca0ec91
Found device:
ModelName: Freesc Dpws Test Device
ModelNumber: 1.0
FirmwareVersion: alpha
FriendlyName: Service1Soap
SerialNumber: 12345678
Service endpoint found: https://192.168.1.101:8084/2c7ea8f1-d812-402d-afa8-6ca9fa03a2f0
mResponse.AddResult: 7
首次執行 Vista 防火牆會詢問,選擇 Unblock 即可。
DPWS 讓資源受限裝置輕鬆公開/消費 Web Service,隨支援裝置增加應用將更普及。.NET MF 團隊正努力與桌面 WCF 整合,未來將構建裝置與 PC 組成的服務網絡,帶來更豐富應用場景。