WindowsMobile調用WCF服務
Compact WCF允許移動裝置上的應用程式通過WCF服務與桌面PC機(服務端)進行互動。目前暫不支援在移動裝置上釋出WCF服務,裝置端的應用程式通常扮演的是發出服務請求的用戶端的角色。在信道的綁定行為上,目前.NET CF3.5預定義的僅支援BasicHttpBinding和WindowsMobileMailBinding兩種方式:
1、BasicHttpBinding,從本質上來講,基本和原來Web Service的方式一樣,因為它支援在http協定下進行傳統的C/S互操作,用戶端隻需發出一個服務請求并等待回應。
2、WindosMobileMailBinding,這是一個Compact WCF中全新的通信方式,它使用Email作為消息傳輸的載體,提供了一個全雙工的通信通道,允許進行非可靠的雙向異步通信。
是以服務的宿主程序采用BasicHttpBinding方式。
一、服務和宿主程序(服務端)
繼續采用“WCF架構入門-用VS2008建構WCF”中的服務和宿主程序!!

二、Mobile用戶端
2.1 建立代理
用VS2008建立“隻能裝置”工程。
用指令行工具NetCFSvcUtil.exe建立用戶端代理。
netcfsvcutil.exe /language:c# htttp://localhost:8001
會産生MyProcess.cs和CFClientBase.cs兩個檔案。
2.2 調用服務
主要紅色部分是與PC用戶端的差別。
using System;
using System.Windows.Forms;
using System.ServiceModel;
using SMC = System.ServiceModel.Channels;
using System.Net;
namespace mobileClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_calculate_Click(object sender, EventArgs e)
{
SMC.Binding binding = MyProcessClient.CreateDefaultBinding();
string remoteAddress = MyProcessClient.EndpointAddress.Uri.ToString();
remoteAddress = remoteAddress.Replace("localhost", "192.168.18.102");
EndpointAddress endpoint = new EndpointAddress(remoteAddress);
MyProcessClient client = new MyProcessClient(binding, endpoint);
this.tbResult.Text = client.Add(2,3).ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
代碼:本文中的代碼可以在以下位址中下載下傳 http://download.csdn.net/source/530750