异步调用Restful的WCF服务
2008-07-12 13:32
696 查看
上周在pedramr blog上看到有人问是否能够异步调用Restful的WCF服务,下面便是具体实现异步调用Restful的WCF实现细节。通过本文的学习,有助于如下知识的掌握:
如何设定WCF的Restful支持
如何异步调用Restful的WCF服务
第一步:创建一个解决方案:AsyCallRestfulWcf,该解决方案包含下面四个项目:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization;
namespace AsyCallRestfulWcf.Contracts
IService.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace AsyCallRestfulWcf.Contracts
第三步:在项目AsyCallRestfulWcf.Service中创建服务实现类Service.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel.Web;
namespace AsyCallRestfulWcf.Service
在服务方法中,用System.Threading.Thread.Sleep(5000);模拟一个比较耗时的操作
第四步 实现WCF服务的承载项目:AsyCallRestfulWcf.Host
添加一个应用程序配置文件app.config和代码文件Programe.cs
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="AsyCallRestfulWcf.Service.Service">
<host>
<baseAddresses>
<add baseAddress="http://locahost"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" contract="AsyCallRestfulWcf.Contracts.IService"
behaviorConfiguration="RestfulBehavior" name="webHttpBinding">
</endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="RestfulBehavior">
<webHttp/>
<synchronousReceive/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Programe.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace AsyCallRestfulWcf.Host
在App.config中,要使WCF支持Restful,要使用的binding是webHttpBinding
第五步:实现异步调用的客户端:AsyCallRestfulWcf.HttpClient
添加windows窗体Form1.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace AsyCallRestfulWcf.HttpClient
现在就可以调试浏览了,将解决方案设置成多启动的、
然后F5,出现下面的界面
和
表明运行正常
点击开始调用,等待几秒后,下面的文本框便出现文字
在开始调用之后,点击取消调用可以取消请求
国际惯例,项目代码:
如何设定WCF的Restful支持
如何异步调用Restful的WCF服务
第一步:创建一个解决方案:AsyCallRestfulWcf,该解决方案包含下面四个项目:
项目名称 | 备注 |
AsyCallRestfulWcf.Contracts | WCF服务的契约项目,包含服务契约和数据契约的定义 |
AsyCallRestfulWcf.Service | WCF服务的具体实现 |
AsyCallRestfulWcf.Host | WCF服务的承载 |
AsyCallRestfulWcf.HttpClient | 用Http 的方式异步调用WCF服务客户端 |
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization;
namespace AsyCallRestfulWcf.Contracts
IService.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace AsyCallRestfulWcf.Contracts
第三步:在项目AsyCallRestfulWcf.Service中创建服务实现类Service.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel.Web;
namespace AsyCallRestfulWcf.Service
在服务方法中,用System.Threading.Thread.Sleep(5000);模拟一个比较耗时的操作
第四步 实现WCF服务的承载项目:AsyCallRestfulWcf.Host
添加一个应用程序配置文件app.config和代码文件Programe.cs
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="AsyCallRestfulWcf.Service.Service">
<host>
<baseAddresses>
<add baseAddress="http://locahost"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" contract="AsyCallRestfulWcf.Contracts.IService"
behaviorConfiguration="RestfulBehavior" name="webHttpBinding">
</endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="RestfulBehavior">
<webHttp/>
<synchronousReceive/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Programe.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace AsyCallRestfulWcf.Host
在App.config中,要使WCF支持Restful,要使用的binding是webHttpBinding
第五步:实现异步调用的客户端:AsyCallRestfulWcf.HttpClient
添加windows窗体Form1.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace AsyCallRestfulWcf.HttpClient
现在就可以调试浏览了,将解决方案设置成多启动的、
然后F5,出现下面的界面
和
表明运行正常
点击开始调用,等待几秒后,下面的文本框便出现文字
在开始调用之后,点击取消调用可以取消请求
国际惯例,项目代码:
相关文章推荐
- [原]RESTful Web Service之以HTTP PUT方式调用WCF服务
- Android调用RESTful WCF服务
- WCF:异步调用长时间工作的服务
- WCF系列教程之客户端异步调用服务
- 实例:在Android调用WCF服务(wcf restful)
- WCF初探-11:WCF客户端异步调用服务
- WCF:异步调用长时间工作的服务
- WCF热门问题编程示例(4):WCF客户端如何异步调用WCF服务?
- WCF热门问题编程示例(4):WCF客户端如何异步调用WCF服务?
- Silverlight中异步调用WCF服务,传入回调函数
- wcf异步服务调用参考地址
- 多线程编程学习笔记——异步调用WCF服务
- 【转载】WCF热门问题编程示例(4):WCF客户端如何异步调用WCF服务?
- WCF热门问题编程示例(4):WCF客户端如何异步调用WCF服务?
- SilverLight下用WebRequest异步调用WCF服务
- WCF RESTful 服务+Jquery 客户端 跨域调用 大坑及解决方案汇总
- Restful风格wcf调用4——权限认证
- 一个WCF服务开发与调用的完整示例
- 使用WCF创建Restful服务
- WCF的异步调用