WCF初探-4:WCF消息交换模式之请求与答复模式

来源:互联网 发布:学生管理系统c语言代码 编辑:程序博客网 时间:2024/06/02 10:58

1.请求与答复模式( Request/Reply)

 这种交换模式是使用最多的一中,它有如下特征:

  • 调用服务方法后需要等待服务的消息返回,即便该方法返回 void 类型
  •  相比Duplex来讲,这种模式强调的是客户端的被动接受,也就是说客户端接受到响应后,消息交换就结束了。
  •  在这种模式下,服务端永远是服务端,客户端就是客户端,职责分明。
  • 它是缺省的消息交换模式,设置OperationContract便可以设置为此种消息交换模式

 

接下来我们通过实例来演示一下,参照WCF消息交换模式之单向模式中的例子,我们将代码稍微做一下修改,将总个解决法案的OneWay全部替换为ReqReply,替换后稍作修改,下面是各个类和接口的代码片段

服务接口IReqReply.cs代码如下:

using System.ServiceModel; namespace Service{    [ServiceContract]    public interface IReqReply    {        [OperationContract]        string SayHello(string name);    }}

服务实现ReqReply.cs代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Service{    public class ReqReply:IReqReply    {        public string SayHello(string name)        {           System.Threading.Thread.Sleep(10000);            return "Hello "+name;        }    }}

Host类库中的配置App.config代码如下:

<?xmlversion="1.0"?><configuration>  <system.serviceModel>    <services>      <servicename="Service.ReqReply"behaviorConfiguration="ReqReplyBehavior">        <host>          <baseAddresses>            <add baseAddress="http://127.0.0.1:1234/ReqReply/"/>          </baseAddresses>        </host>         <endpoint address=""binding="wsHttpBinding" contract="Service.IReqReply"/>        <endpoint address="mex"binding="mexHttpBinding" contract="IMetadataExchange"/>      </service>    </services>     <behaviors>      <serviceBehaviors>        <behaviorname="ReqReplyBehavior">          <serviceMetadatahttpGetEnabled="True"/>          <serviceDebugincludeExceptionDetailInFaults="True"/>        </behavior>      </serviceBehaviors>    </behaviors>  </system.serviceModel> </configuration>

Host类库中的配置Program.cs代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;using Service; namespace Host{    class Program    {        static void Main(string[] args)        {            using (ServiceHost ReqReplyHost =new ServiceHost(typeof(ReqReply)))            {                ReqReplyHost.Opened += delegate                {                    Console.WriteLine("请求响应通讯服务已经启动,按任意键终止!");                };                 ReqReplyHost.Open();                Console.Read();            }        }    }}

整个解决方案工程结构没有变化,只是服务方法做了修改,通过休眠线程的时间和返回值来观察客户端对服务端调用的变化。编译程序后,我们运行Host.exe寄宿程序寄宿该服务。添加客户端的服务引用:

 

 

然后在客户端控制台程序Program.cs中添加如下代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Client.ReqReplyServiceRef; namespace Client{    class Program    {        static void Main(string[] args)        {           Console.WriteLine("****************请求响应通讯服务示例*******************");            ReqReplyClient proxy = newReqReplyClient();            Console.WriteLine("方法调用前时间:"+ System.DateTime.Now);           Console.WriteLine(proxy.SayHello("WCF"));           Console.WriteLine("方法调用后时间:" + System.DateTime.Now);            Console.Read();        }    }}

编译后运行Client.exe程序可以看到以下结果:

 

我们可以看到服务器响应的时间刚好为10s,正好是线程休眠的时间,并且客户端返回了信息Hello WCF ,如果想要观察消息的变化,请参照WCF消息交换模式之单向模式中的WCF客户端测试程序使用方法,观察消息的变化。

此文只对平时学习的一个积累,如有不当之处,欢迎指出,共同学习进步。

 

作者:wangweimutou
出处:http://blog.csdn.net/wangweimutou/article/details/32327165
本文版权归作者和CSDN共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

 

0 0
原创粉丝点击