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