Windows Phone 7 网络编程之调用WCF Service

简介:

一、WCF Service简介

WCF(Windows Communication Foundation)是Microsoft为构建面向服务的应用提供的分布式通信编程框架,使用该框架,开发人员可以构建跨平台、安全、可靠和支持事务处理的企业级互联应用解决方案。WCF是微软整合了微软分布式应用程序开发中的众多成熟技术,如Enterprise Sevices(COM+)、.Net Remoting、Web
Service(ASMX)、WSE和MSMQ消息队列。

通讯范围:可以跨进程、跨机器、跨子网、企业网乃至于Internet;

宿主:可以是ASP.NET(IIS或WAS),EXE,WPF,Windows Forms,NT Service,COM+;

通信协议:TCP,HTTP,跨进程以及自定义。


 

二、 创建WCF Service

下面演示一下如何搭建一个WCF服务,简单地输出一个字符串。

第一步:打开visual studio 2010创建一个WCF服务应用程序。

 

 

第二步:修改Service1.svc和IService1.cs文件。

创建好的项目默认的文件目录如下:

 

 

在Service1.svc中添加方法

public string HelloWCF() 

return "Hello WCF";

}

在IService1.cs中添加接口:

[OperationContract]
string HelloWCF();


 

第三步:创建一个网站的虚拟目录指向WCF的项目工程。

在控制面板中找到Internet信息管理器并打开,在Default Web Site节点下创建一个虚拟目录,命名为wcf,路径指向本例子的web应用程序的代码,并点击确定。

三、 调用WCF Service

第一步:在Windows Phone项目中添加WCF服务引用,生成代理。

引用成功后会产生一个ServiceReferences.ClientConfig文件,文件如下:

<configuration>

<system.serviceModel>

<bindings>

<basicHttpBinding>

<binding
name="BasicHttpBinding_IService1"
maxBufferSize="2147483647"

maxReceivedMessageSize="2147483647">

<security
mode="None" />

</binding>


</basicHttpBinding>

</bindings>
<client>

<!--http://localhost/wcf/Service1.svc是指WCF服务的地址--> 
<endpoint address="http://localhost/wcf/Service1.svc"
binding="basicHttpBinding" 
bindingConfiguration="BasicHttpBinding_IService1" contract="WCFService.IService1" 
name="BasicHttpBinding_IService1" /> 
</client> 
</system.serviceModel>

</configuration>

第二步:调用WCF服务。 
WCFDemo.WCFService.Service1Client proxy = new WCFService.Service1Client(); 
proxy.HelloWCFCompleted += new EventHandler<WCFService.HelloWCFCompletedEventArgs>(proxy_HelloWCFCompleted); 
proxy.HelloWCFAsync();


 


void proxy_HelloWCFCompleted(object sender,
WCFService.HelloWCFCompletedEventArgs e)

textBlock1.Text = e.Result.ToString(); 
}

XAML代码

<Grid x:Name="ContentPanel" Grid.Row="1"
Margin="12,0,12,0"> 
<TextBlock
Height="63" HorizontalAlignment="Left"
Margin="60,47,0,0" Name="textBlock1" Text=""
VerticalAlignment="Top" Width="249" /> 
</Grid>

运行效果:

 

 

本文转自linzheng 51CTO博客,原文链接:

http://blog.51cto.com/linzheng/1078592

相关文章
|
2月前
|
Linux C++ Windows
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
|
17天前
|
缓存 监控 网络协议
计算机网络的常用的网络通信命令(Windows)
本文介绍了网络技术中常用的命令,如ping用于检测网络连通性,ipconfig查看TCP/IP配置,netstat监控网络状态,arp显示和修改ARP缓存,at安排任务执行,tracert追踪路由,以及nbtstat获取NetBIOS信息。
18 1
|
28天前
|
安全 Windows
怎样利用 Windows XP实现网络统一关机
怎样利用 Windows XP实现网络统一关机
怎样利用 Windows XP实现网络统一关机
|
2月前
|
Java 应用服务中间件 开发工具
[App Service for Windows]通过 KUDU 查看 Tomcat 配置信息
[App Service for Windows]通过 KUDU 查看 Tomcat 配置信息
|
2月前
|
Java 应用服务中间件 Windows
【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
|
2月前
|
PHP Windows
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
|
2月前
|
PHP 开发工具 git
【Azure 应用服务】在 App Service for Windows 中自定义 PHP 版本的方法
【Azure 应用服务】在 App Service for Windows 中自定义 PHP 版本的方法
|
2月前
|
网络安全 API 数据安全/隐私保护
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
|
2月前
【Azure App Service】列举为App Service集成虚拟网络(VNET)操作时所需要的最小权限
【Azure App Service】列举为App Service集成虚拟网络(VNET)操作时所需要的最小权限
|
2月前
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.

热门文章

最新文章