前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WCF系列教程之消息交换模式之请求与答复模式(Request/Reply)

WCF系列教程之消息交换模式之请求与答复模式(Request/Reply)

作者头像
郑小超.
发布2018-01-26 14:47:40
4970
发布2018-01-26 14:47:40
举报
文章被收录于专栏:GreenLeavesGreenLeaves

1、使用WCF请求与答复模式须知

(1)、客户端调用WCF服务端需要等待服务端的返回,即使返回类型是void

(2)、相比Duplex来讲,这种模式强调的是客户端的被动接受,也就是说客户端接受到响应后,消息交换就结束了

(3)、在这种模式下,服务端永远是服务端,客户端就是客户端,职责分明。

(4)、它是缺省的消息交换模式,设置OperationContract便可以设置为此种消息交换模式

2、代码示例

服务层接口IReqReplyService.cs代码如下:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace IService
{
    [ServiceContract]
    public interface IReqReplyService
    {
        [OperationContract]
        void HelloWorld(string name);
    }
}

服务层接口IReqReplyService.cs的实现类代码如下:

代码语言:javascript
复制
using IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Service
{
    public class ReqReplyService : IReqReplyService
    {
        public void HelloWorld(string name)
        {
            Thread.Sleep(6000);
        }
    }
}

修改宿主配置文件App.config如下:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="Service.ReqReplyService" behaviorConfiguration="OneWayBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/ReqReplyService/"/>
          </baseAddresses>
        </host>

        <endpoint address="" binding="wsHttpBinding" contract="IService.IReqReplyService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
      </services>


    <behaviors>
      <serviceBehaviors>
        <behavior name="OneWayBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="True"/>
          </behavior>
        </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

生成解决方案,开启宿主服务,并通过微软的svcutil工具生成ReqReplyService服务的客户端代理类,开始菜单/Microsoft Visual Studio 2012/Visual Studio Tools/Visual Studio 2012开发人员命令提示工具,定位到当前客户端

l路径,输入命令:svcutil http://localhost:8000/ReqReplyService/?wsdl  /o:ReqReplyService.cs,生成客户端代理类,生成成功之后,将文件添加到项目中.

修改客户端调用方法,如下:

代码语言:javascript
复制
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();
        }
    }
}

我们可以看到服务器响应的时间刚好为6s,正好是线程休眠的时间,并且客户端返回了信息Hello WCF.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-05-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档