zoukankan      html  css  js  c++  java
  • WCF初探-4:WCF消息交换模式之请求与答复模式

    请求与答复模式( 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
    关注技能:ASP.NET、ASP.NET MVC、ASP.NET WEB API、WCF、MSSQL、ORACLE
    感谢您阅读本文,如果您觉得有所收获,麻烦点一下右边的【推荐】,您的支持是对我最大的鼓励!
    由于作者能力有限,本文只做学习交流使用,如有不正之处,欢迎指出
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留原文链接,否则保留追究法律责任的权利
  • 相关阅读:
    蓝桥网试题 java 基础练习 特殊的数字
    蓝桥网试题 java 基础练习 杨辉三角形
    蓝桥网试题 java 基础练习 查找整数
    蓝桥网试题 java 基础练习 数列特征
    蓝桥网试题 java 基础练习 字母图形
    蓝桥网试题 java 基础练习 01字串
    蓝桥网试题 java 基础练习 回文数
    蓝桥网试题 java 基础练习 特殊回文数
    Using text search in Web page with Sikuli
    each of which 用法
  • 原文地址:https://www.cnblogs.com/wangweimutou/p/4086614.html
Copyright © 2011-2022 走看看