首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

.NET Micro Framework 開發 DPWS 裝置服務完整實作指南

     編輯:關於.NET

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 架構與類庫總覽

DPWS 架構圖

DPWS Host 類庫 (MFDpwsDevice.dll、MFWsStack.dll)

命名空間說明組件
Dpws.DeviceDPWS 裝置堆疊核心類別、屬性控制MFDpwsDevice.dll
Dpws.Device.Services事件來源、主機服務端點/操作/事件管理MFDpwsDevice.dll
Ws.Services端點服務操作管理MFWsStack.dll
Ws.Services.Faults錯誤處理類別MFWsStack.dll
Ws.Services.MtomSOAP MTOM 處理MFWsStack.dll
Ws.Services.SoapSOAP 訊息解析MFWsStack.dll
Ws.Services.Transport基礎網路傳輸服務MFWsStack.dll
Ws.Services.Utilities除錯輸出、持續時間、UUID 驗證MFWsStack.dll
Ws.Services.WsaAddressingWS-Addressing 標頭處理MFWsStack.dll
Ws.Services.XmlXML 處理工具MFWsStack.dll

DPWS Client 類庫 (MFDpwsClient.dll)

命名空間說明
Dpws.ClientDPWS 客戶端建立
Dpws.Client.Discovery查找與解析請求
Dpws.Client.Eventing事件訂閱客製化
Dpws.Client.TransportHTTP 請求發送/接收

完整開發流程實作

步驟 1:定義 Web Service WSDL

建立 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。

步驟 2:MfSvcUtil.exe 產生輔助類別

SDK 工具目錄執行:

MfSvcUtil AddService.wsdl

產生三個檔案:

  • AddService.cs - 資料合約類別
  • AddServiceHostedService.cs - 主機服務基類
  • AddServiceClientProxy.cs - 客戶端 Proxy

步驟 3:建立 Device Host Service

新增 .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);
    }
}

步驟 4:建立 Device Client Consumer

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
    }
}

步驟 5:執行驗證

雙 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 組成的服務網絡,帶來更豐富應用場景。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved