专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > WCF

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

发布时间:2011-06-23 13:51:47 文章来源:www.iduyao.cn 采编人员:星星草
WCF初探-4:WCF消息交换模式之请求与答复模式

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客户端测试程序使用方法,观察消息的变化。

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: