Microsoft Mobile访问WCF服务的例子

http://www.c-sharpcorner.com/uploadfile/sry_143/using-wcf-in-windows-mobile-application/   这个例子可能更适合你

 

本文将介绍在PC上建立一个WCF服务,然后建立一个手机客户端程序来访问该WCF服务。涉及到的问题如下:

1.       如何在在PC上建立WCF服务,如何配置该服务;

2.       Window Mobile中如何正确访问网路,即网络的配置;

3.       最后,建立手机在Window Mobile中正确访问该WCF服务

 
本文的测试环境 VS2008, Windows2008
 
首先,在PC上建立WCF服务
这里建立一个简单的WCF服务,返回一句HelloWord。
首先建立一个服务契约:
服务契约
  1. [ServiceContract]   
  2.  public interface ILoginService   
  3.  {   
  4.      ///<summary>   
  5.      /// HelloWord!   
  6.      ///</summary>   
  7.      ///<returns></returns>   
  8.      [OperationContract]   
  9.      string GetHello(); 
 
 
实现服务:
实现服务
  1. [ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.PerCall)]   
  2. public class LoginService:ILoginService   
  3. {   
  4.     string ILoginService.GetHello()   
  5.     {   
  6.         return "Hello World! Welcome to 百洋软件研究实验室!";   
  7.     }   
  8. }  
 
WCF配置:
XML/HTML代码
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.     <system.serviceModel>  
  4.       <behaviors>  
  5.         <serviceBehaviors>  
  6.           <behavior name="MyServiceTypeBehaviors">  
  7.             <serviceMetadata httpGetEnabled="true"  httpGetUrl="http://localhost:8888/LoginService" />  
  8.           </behavior>  
  9.         </serviceBehaviors>  
  10.       </behaviors>  
  11.          
  12.         <services>  
  13.             <service name="WCFDemo.LoginService" behaviorConfiguration="MyServiceTypeBehaviors">  
  14.                 <endpoint address="" binding="basicHttpBinding" contract="WCFDemo.ILoginService"/>                 
  15.                 <host>  
  16.                     <baseAddresses>  
  17.                         <add baseAddress="http://localhost:8888/LoginService" />                      
  18.                     </baseAddresses>  
  19.                 </host>  
  20.             </service>  
  21.         </services>  
  22.     </system.serviceModel>  
  23. </configuration>  
 
测试WCF 服务是否发布成功
 
在浏览器中输入 http://localhost:8888/LoginService 回车,如果出现如下的界面表示发布成功。
Login服务
到此WCF服务建立完毕,下面是建立手机应用程序,并调用该WCF服务程序。
 
配置移动设备能够上网
 
下面,配置移动设备的联网状况,使之能够访问PC上的WCF服务。Vista或2008
 
1. 安装Microsoft Virtual PC 2007或以上版本
 
安装Microsoft Virtual PC 2007的目的就是安装Virtual Machine Network Services Driver。下载地址: http://www.microsoft.com/downloads/details.aspx?FamilyId=04D26402-3199-48A3-AFA2-2DC0B40A73B6&displaylang=en
安装 Microsoft Windows Mobile Device Center 6 for Windows Vista (32-bit) - 简体中文。下载地址: http://www.microsoft.com/downloads/details.aspx?familyid=83d513ea-9df9-4920-af33-3a0e2e4e7beb&displaylang=zh-cn
安装完成后,在控制面板会出现一个图标。
图标
 
3. 开始连接上网
4. 运行Visual Studio 2008,打开“工具 -> 设备仿真器管理器”,右键单击“CHS Windows Mobile 5.0 Pocket PC R2 Emulator”,选择“连接”,然后选择“插入底座”。
管理器
 
管理器
5. 配置Windows Mobile 5.0 emulator
这个时候Windows Mobile 5.0 emulator已经打开了,你可以看到一个很真实的Windows Mobile 5.0模拟器界面,当然是多了一些菜单的。选择“文件 -> 配置”,选择“网络”选项卡,选中“启用 NE2000 PCMCIA 网络适配器并绑定到(N)”,下拉菜单中选择“连接的网卡”或者你当时用来上网那块网卡。
属性
 
6. 配置Windows Mobile 设备中心
移动设备第一次插入PC的时候,会自动弹出设置窗口,或者在控制面板中,点击mobile 设备中心,手动配置。本文按照下面配置。
设置
下图是连接成功后
成功
7. 配置Windows Mobile 5.0系统IP地址
这个时候就是在Windows Mobile 5.0系统中配置了,“开始 -> 设置 -> 连接(选项卡) -> 网卡”,我的网卡连接到“默认单位设置”,下面选择“NE2000 兼容 Ethernet 驱动程序”,输入一个IP地址就行了,根据你的网络来输入即可。然后点击“ok”保存关闭。然后在Windows Mobile 5.0 emulator找到“文件 -> 保存状态并退出”。再次启动Windows Mobile 5.0 emulator,你进入Windows Mobile 5.0打开IE浏览器,看看是不是能上网了?!
 
建立一个移动设备应用程序
新建->项目->智能设备项目。
移动设备调用PC WCF服务
首先 ,下载一个工具,Power Toys for .NET Compact Framework 3.5 , 下载地址: http://www.microsoft.com/downloads/details.aspx?FamilyID=c8174c14-a27d-4148-bf01-86c2e0953eab&displaylang=en  该工具能够自动生成WCF的代理类。
    安装完成后,打开目录找到
C:/Program Files/Microsoft.NET/SDK/CompactFramework/v3.5/bin 文件夹下的 netcfSvcUtil.exe 文件。 在该目录建立一个 bat 文件,或者使用 dos 命令,转到该目录。   
netcfSvcUtil.exe /language:cs http://192.168.1.2:8888/LoginService
显示
http://192.168.1.2:8888/LoginService 是我们WCF元数据的发布地址,当然了前提是WCF服务已经在运行。
运行之后,在该目录下 生成两个文件: LogService.cs( 我们的服务代理类 ), 另一个 CFClientBase.cs 文件。将这两个文件附加到,刚建立的移动设备工程中去。
Form 中添加一 Button
button 的双击打开事件中,调用 WCF.
C#代码
  1. using System;   
  2. using System.Linq;   
  3. using System.Collections.Generic;   
  4. using System.ComponentModel;   
  5. using System.Data;   
  6. using System.Drawing;   
  7. using System.Text;   
  8. using System.Windows.Forms;   
  9. using SMC = System.ServiceModel.Channels;   
  10. using System.ServiceModel;   
  11.     
  12. namespace SmartDeviceProject2   
  13. {   
  14.     public partial class Form1 : Form   
  15.     {   
  16.         public Form1()   
  17.         {   
  18.             InitializeComponent();   
  19.         }   
  20.     
  21.         private void button1_Click(object sender, EventArgs e)   
  22.         {   
  23.             SMC.Binding binding = LoginServiceClient.CreateDefaultBinding();   
  24.             string remoteAddress = LoginServiceClient.EndpointAddress.Uri.ToString();   
  25.             remoteAddress = remoteAddress.Replace("localhost""192.168.1.2");   
  26.             EndpointAddress endpoint = new EndpointAddress(remoteAddress);   
  27.             LoginServiceClient client = new LoginServiceClient(binding, endpoint);   
  28.             MessageBox.Show(client.GetHello());   
  29.         }   
  30.     }   
  31. }   
 最后部署,到该移动设备。
预览 
后记,该移动设备程序调用WCF,从服务端 元数据的开放地址获取,生成代理类, 来使用。
 
附上源码。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值