您的位置:首页 > 其它

异步调用Restful的WCF服务

2008-07-12 13:32 696 查看
上周在pedramr blog上看到有人问是否能够异步调用Restful的WCF服务,下面便是具体实现异步调用Restful的WCF实现细节。通过本文的学习,有助于如下知识的掌握:

如何设定WCF的Restful支持

如何异步调用Restful的WCF服务

第一步:创建一个解决方案:AsyCallRestfulWcf,该解决方案包含下面四个项目:

项目名称

备注

AsyCallRestfulWcf.Contracts

WCF服务的契约项目,包含服务契约和数据契约的定义

AsyCallRestfulWcf.Service

WCF服务的具体实现

AsyCallRestfulWcf.Host

WCF服务的承载

AsyCallRestfulWcf.HttpClient

用Http 的方式异步调用WCF服务客户端

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,出现下面的界面







表明运行正常

点击开始调用,等待几秒后,下面的文本框便出现文字



在开始调用之后,点击取消调用可以取消请求

国际惯例,项目代码:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: