Microsoft Mobile访问WCF服务的例子
2011-02-16 11:10
267 查看
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。
首先建立一个服务契约:
服务契约
[ServiceContract]
public interface ILoginService
{
///<summary>
/// HelloWord!
///</summary>
///<returns></returns>
[OperationContract]
string GetHello();
}
实现服务:
实现服务
[ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.PerCall)]
public class LoginService:ILoginService
{
string ILoginService.GetHello()
{
return "Hello World! Welcome to 百洋软件研究实验室!";
}
}
WCF配置:
XML/HTML代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8888/LoginService" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="WCFDemo.LoginService" behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="" binding="basicHttpBinding" contract="WCFDemo.ILoginService"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8888/LoginService" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
测试WCF 服务是否发布成功
在浏览器中输入 http://localhost:8888/LoginService 回车,如果出现如下的界面表示发布成功。
到此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#代码
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SMC = System.ServiceModel.Channels;
using System.ServiceModel;
namespace SmartDeviceProject2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SMC.Binding binding = LoginServiceClient.CreateDefaultBinding();
string remoteAddress = LoginServiceClient.EndpointAddress.Uri.ToString();
remoteAddress = remoteAddress.Replace("localhost", "192.168.1.2");
EndpointAddress endpoint = new EndpointAddress(remoteAddress);
LoginServiceClient client = new LoginServiceClient(binding, endpoint);
MessageBox.Show(client.GetHello());
}
}
}
最后部署,到该移动设备。
后记,该移动设备程序调用WCF,从服务端 元数据的开放地址获取,生成代理类, 来使用。
附上源码。
SmartPhone.rar
这个例子可能更适合你
本文将介绍在PC上建立一个WCF服务,然后建立一个手机客户端程序来访问该WCF服务。涉及到的问题如下:
1. 如何在在PC上建立WCF服务,如何配置该服务;
2. Window Mobile中如何正确访问网路,即网络的配置;
3. 最后,建立手机在Window Mobile中正确访问该WCF服务
本文的测试环境 VS2008, Windows2008
首先,在PC上建立WCF服务
这里建立一个简单的WCF服务,返回一句HelloWord。
首先建立一个服务契约:
服务契约
[ServiceContract]
public interface ILoginService
{
///<summary>
/// HelloWord!
///</summary>
///<returns></returns>
[OperationContract]
string GetHello();
}
实现服务:
实现服务
[ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.PerCall)]
public class LoginService:ILoginService
{
string ILoginService.GetHello()
{
return "Hello World! Welcome to 百洋软件研究实验室!";
}
}
WCF配置:
XML/HTML代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8888/LoginService" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="WCFDemo.LoginService" behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="" binding="basicHttpBinding" contract="WCFDemo.ILoginService"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8888/LoginService" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
测试WCF 服务是否发布成功
在浏览器中输入 http://localhost:8888/LoginService 回车,如果出现如下的界面表示发布成功。
到此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#代码
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SMC = System.ServiceModel.Channels;
using System.ServiceModel;
namespace SmartDeviceProject2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SMC.Binding binding = LoginServiceClient.CreateDefaultBinding();
string remoteAddress = LoginServiceClient.EndpointAddress.Uri.ToString();
remoteAddress = remoteAddress.Replace("localhost", "192.168.1.2");
EndpointAddress endpoint = new EndpointAddress(remoteAddress);
LoginServiceClient client = new LoginServiceClient(binding, endpoint);
MessageBox.Show(client.GetHello());
}
}
}
最后部署,到该移动设备。
后记,该移动设备程序调用WCF,从服务端 元数据的开放地址获取,生成代理类, 来使用。
附上源码。
附件:
WCFDemo.rarSmartPhone.rar
相关文章推荐
- Microsoft Mobile访问WCF服务的例子
- VS2008下做的一个WCF服务的例子
- WCF客户端无法访问服务原因之一时间不同步问题
- Android访问WCF服务
- WCF 一步一步 编写访问 WCF服务 的客户端程序
- WCF 一步一步 编写访问 WCF服务 的客户端程序
- WCF服务发布到IIS6.0跨域访问的解决方法
- 客户端使用自定义代理类访问WCF服务
- 快速访问WCF服务--ServiceModel 元数据实用工具 (Svcutil.exe)
- 用雇凶杀人的例子来理解WCF中服务、宿主、客户端关系与配置
- WCF服务以控制台程序为宿主时的跨域访问问题(附源码)
- C#应用访问Microsoft SQL Server 2005分析服务
- wcf创建基于tcp协议的iis寄宿服务的第一个例子
- 未能创建SSL/TLS安全通道,导致的通过HTTPS协议访问WCF服务
- WCF服务用户名密码访问
- Microsoft .NET Framework 4.5.1 无法安装(Win7无法访问Windows Installer服务的解决方法)
- WCF 添加服务失败。服务元数据可能无法访问。请确保服务正在运行并且正在公开元数据
- Android访问WCF服务
- WCF 一步一步 编写访问 WCF服务 的客户端程序
- WCF 第十一章 工作流服务 控制对服务开启的工作流的访问