消息处理(异步调用OneWay, 双向通讯Duplex)

转自 http://www.cnblogs.com/webabcd/archive/2008/04/14/1153027.html

介绍
WCF(Windows Communication Foundation) - 消息处理:通过操作契约的IsOneWay参数实现异步调用,基于Http, TCP, Named Pipe, MSMQ的双向通讯。

示例(异步调用OneWay)
1、服务
IOneWay.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;

namespace WCF.ServiceLib.Message
{
   /// <summary>
    /// IOneWay接口
    /// </summary>
    [ServiceContract]
    public interface IOneWay
    {
        /// <summary>
        /// 不使用OneWay(同步调用)
        /// </summary>
        [OperationContract]
        void WithoutOneWay();

        /// <summary>
        /// 使用OneWay(异步调用)
        /// </summary>
        [OperationContract(IsOneWay=true)]
        void WithOneWay();
    }
}

OneWay.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;

namespace WCF.ServiceLib.Message
{
    /// <summary>
    /// OneWay类
    /// </summary>
    public class OneWay : IOneWay
    {
        /// <summary>
        /// 不使用OneWay(同步调用)
        /// 抛出Exception异常
        /// </summary>
        public void WithoutOneWay()
        {
            throw new System.Exception("抛出Exception异常");
        }

        /// <summary>
        /// 使用OneWay(异步调用)
        /// 抛出Exception异常
        /// </summary>
        public void WithOneWay()
        {
            throw new System.Exception("抛出Exception异常");
        }
    }
}

2、宿主
OneWay.cs

using (ServiceHost host = new ServiceHost(typeof(WCF.ServiceLib.Message.OneWay)))
{
    host.Open();

    Console.WriteLine("服务已启动(WCF.ServiceLib.Message.OneWay)");
    Console.WriteLine("按<ENTER>停止服务");
    Console.ReadLine();

}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <!--name - 提供服务的类名-->
      <!--behaviorConfiguration - 指定相关的行为配置-->
      <service name="WCF.ServiceLib.Message.OneWay" behaviorConfiguration="MessageBehavior">
        <!--address - 服务地址-->
        <!--binding - 通信方式-->
        <!--contract - 服务契约-->
        <endpoint address="" binding="basicHttpBinding" contract="WCF.ServiceLib.Message.IOneWay" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:12345/Message/OneWay/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MessageBehavior">
          <!--httpGetEnabled - 使用get方式提供服务-->
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

3、客户端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using System.ServiceModel;

namespace Client2.Message
{
    /// <summary>
    /// 演示Message.OneWay的类
    /// </summary>
    public class OneWay
    {
        /// <summary>
        /// 调用IsOneWay=true的操作契约(异步操作)
        /// </summary>
        public void HelloWithOneWay()
        {
            try
            {
                var proxy = new MessageSvc.OneWay.OneWayClient();

                proxy.WithOneWay();

                proxy.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 调用IsOneWay=false的操作契约(同步操作)
        /// </summary>
        public void HelloWithoutOneWay()
        {
            try
            {
                var proxy = new MessageSvc.OneWay.OneWayClient();

                proxy.WithoutOneWay();

                proxy.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <client>
      <!--address - 服务地址-->
      <!--binding - 通信方式-->
      <!--contract - 服务契约-->
      <endpoint address="http://localhost:12345/Message/OneWay/" binding="basicHttpBinding" contract="MessageSvc.OneWay.IOneWay" />
    </client>
  </system.serviceModel>
</configuration>

运行结果:
单击"btnWithOneWay"按钮,没有弹出提示框。(异步操作)
单击"btnWithoutOneWay"按钮,弹出错误提示框。(同步操作)

示例(双向通讯Duplex)
1、服务
IDuplex.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;

namespace WCF.ServiceLib.Message
{
    /// <summary>
    /// IDuplex接口
    /// </summary>
    /// <remarks>
    /// CallbackContract - 回调接口
    /// </remarks>
    [ServiceContract(CallbackContract = typeof(IDuplexCallback))]
    public interface IDuplex
    {
        /// <summary>
        /// Hello
        /// </summary>
        /// <param name="name">名字</param>
        [OperationContract(IsOneWay = true)]
        void HelloDuplex(string name);
    }

    /// <summary>
    /// IDuplex回调接口
    /// </summary>
    public interface IDuplexCallback
    {
        /// <summary>
        /// Hello
        /// </summary>
        /// <param name="name"></param>
        [OperationContract(IsOneWay = true)]
        void HelloDuplexCallback(string name);
    }
}

Duplex.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;

namespace WCF.ServiceLib.Message
{
    /// <summary>
    /// Duplex类
    /// </summary>
    public class Duplex : IDuplex
    {
        /// <summary>
        /// Hello
        /// </summary>
        /// <param name="name">名字</param>
        public void HelloDuplex(string name)
        {
            // 声明回调接口
            IDuplexCallback callback = OperationContext.Current.GetCallbackChannel<IDuplexCallback>();
            
            // 调用回调接口中的方法
            callback.HelloDuplexCallback(name);
        }
    }
}

2、宿主
Duplex.cs

using (ServiceHost host = new ServiceHost(typeof(WCF.ServiceLib.Message.Duplex)))
{
    host.Open();

    Console.WriteLine("服务已启动(WCF.ServiceLib.Message.Duplex)");
    Console.WriteLine("按<ENTER>停止服务");
    Console.ReadLine();

}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <!--name - 提供服务的类名-->
      <!--behaviorConfiguration - 指定相关的行为配置-->
      <service name="WCF.ServiceLib.Message.Duplex" behaviorConfiguration="MessageBehavior">
        <!--address - 服务地址-->
        <!--binding - 通信方式-->
        <!--contract - 服务契约-->
        <!--双向通讯可以基于Http, TCP, Named Pipe, MSMQ;其中基于Http的双向通讯会创建两个信道(Channel),即需要创建两个http连接-->
        <!--endpoint address="Message/Duplex" binding="wsDualHttpBinding" contract="WCF.ServiceLib.Message.IDuplex" /-->
        <endpoint address="Message/Duplex" binding="netTcpBinding" contract="WCF.ServiceLib.Message.IDuplex" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:12345/Message/Duplex"/>
            <add baseAddress="net.tcp://localhost:54321/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MessageBehavior">
          <!--httpGetEnabled - 使用get方式提供服务-->
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

3、客户端
Duplex.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.Windows.Forms;

namespace Client2.Message
{
    /// <summary>
    /// 演示Message.Duplex的类
    /// </summary>
    public class Duplex
    {
        /// <summary>
        /// Hello
        /// </summary>
        /// <param name="name">名字</param>
        public void HelloDulex(string name)
        {
            var ct = new Client2.Message.CallbackType();
            var ctx = new InstanceContext(ct);

            var proxy = new MessageSvc.Duplex.DuplexClient(ctx);

            proxy.HelloDuplex(name);
        }
    }
}

CallbackType.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace Client2.Message
{
    /// <summary>
    /// 实现回调接口
    /// </summary>
    public class CallbackType : MessageSvc.Duplex.IDuplexCallback
    {
        /// <summary>
        /// Hello
        /// </summary>
        /// <param name="name">名字</param>
        public void HelloDuplexCallback(string name)
        {
            MessageBox.Show("Hello: " + name);
        }
    }
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <client>
      <!--address - 服务地址-->
      <!--binding - 通信方式-->
      <!--contract - 服务契约-->
      <!--endpoint address="http://localhost:12345/Message/Duplex/" binding="wsDualHttpBinding" contract="MessageSvc.Duplex.IDuplex" /-->
      <endpoint address="net.tcp://localhost:54321/Message/Duplex" binding="netTcpBinding" contract="MessageSvc.Duplex.IDuplex" />
    </client>
  </system.serviceModel>
</configuration>

运行结果:
单击"btnDuplex"按钮后弹出提示框,显示"Hello: webabcd"

转载于:https://www.cnblogs.com/webglcn/archive/2012/05/03/2479902.html

消息处理(异步调用OneWay, 双向通讯Duplex)相关推荐

  1. 我的WCF之旅 (11): 再谈WCF的双向通讯-基于Http的双向通讯 V.S. 基于TCP的双向通讯...

    在一个基于面向服务的分布式环境中,借助一个标准的.平台无关的Communication Infrastructure,各个Service通过SOAP Message实现相互之间的交互.这个交互的过程实 ...

  2. WCF简单教程(6) 单向与双向通讯

    第六篇:单向与双向通讯 项目开发中我们时常会遇到需要异步调用的问题,有时忽略服务端的返回值,有时希望服务端在需要的时候回调,今天就来看看在WCF中如何实现. 先看不需要服务端返回值的单向调用,老规矩, ...

  3. 软件中的1、同步调用;2、回调;3、异步调用

    软件模块中存在一定接口,从调用方式上分为三类 1.同步调用:2.回调:3.异步调用 首先,同步调用是一种阻塞式调用,调用方要等待对象执行完毕才返回.它是一种单向调用. 其次,回调是一种双向调用模式,也 ...

  4. 安卓通讯之《蓝牙单片机通讯助手》②扫描设备、连接设备和双向通讯。

    前言 上篇文章我们介绍到了开发经典蓝牙和单片机通讯的过程,安卓通讯之<蓝牙单片机通讯助手>①集成工作 ,我们这里还要兼容最新的安卓6.0及以上的系统,因为从6.0以后的权限机制和以往的不一 ...

  5. Dubbo 同步、异步调用的几种方式

    我们知道,Dubbo 缺省协议采用单一长连接,底层实现是 Netty 的 NIO 异步通讯机制:基于这种机制,Dubbo 实现了以下几种调用方式: 同步调用 异步调用 参数回调 事件通知 同步调用 同 ...

  6. Direct3D Draw函数 异步调用原理解析

    概述 在D3D10中,一个基本的渲染流程可分为以下步骤: 清理帧缓存: 执行若干次的绘制: 通过Device API创建所需Buffer: 通过Map/Unmap填充数据到Buffer中: 将Buff ...

  7. java同步异步调用_详解java 三种调用机制(同步、回调、异步)

    1:同步调用:一种阻塞式调用,调用方要等待对方执行完毕才返回,jsPwwCe它是一种单向调用 2:回调:一种双向调用模式,也就是说,被调用方在接口被调用时也会调用对方的接口: 3:异步调用:一种类似消 ...

  8. 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(2):处理异步调用中的异常...

    本文来自<ASP.NET AJAX程序设计 第II卷:客户端Microsoft AJAX Library相关>的第三章<异步调用Web Service和页面中的类方法>,请同时 ...

  9. java同步转化成异步_Java 如何把异步调用模拟成同步调用

    在某些时候,须要把异步调用模拟成同步调用的形态.例如,基于基于异步通讯的客户端须要同步调用. :-)异步 要实现这个转换,能够有多种实现方法:this 1.很经常使用的方法,应用循环机制:spa bo ...

  10. scala 异步调用_非阻塞异步Java 8和Scala的Try / Success / Failure

    scala 异步调用 受Heinz Kabutz最近的时事通讯以及我在最近的书中研究的Scala的期货的启发,我着手使用Java 8编写了一个示例,该示例如何将工作提交给执行服务并异步地响应其结果,并 ...

最新文章

  1. c++对象模型之Data布局
  2. GridView列表数据的添加
  3. 回家 Bessie Come Home
  4. C++中vector容器为什么扩容时按照2倍或者1.5倍进行扩容
  5. mac 由于网络问题,您已断开与 windows 计算机的联接.,苹果电脑启用windows系统时连接不上无线网怎么处理?...
  6. 【Elasticsearch】追踪同步分片副本 in-sync allocation IDs
  7. Oracle数据库开机自启动
  8. leetcode946. Validate Stack Sequences
  9. Windows 2003系统安全+IIS下Web与FTP的完美结合(下)
  10. Greenrobot-EventBus源码学习(四)
  11. JAVA中this的四种用法的详解
  12. ipad上html语言编辑,Html编辑器iPad版
  13. suse linux安装rpm包,suse linux rpm 安装
  14. andorid 源码北京公交线路查询(离线)
  15. 安装win10 ltsc应用商店
  16. 年纪一大把,胡子一大堆,还能学好编程吗?今天我问了我自己
  17. 阿里云虚拟机转让(RAM创建账户)
  18. 数据仓库之数据质量管理
  19. WIN10 设置自动定时开机
  20. 100教程-100jc.cn

热门文章

  1. pandas.DataFrame将行(index)和列(column)进行转置
  2. ffmpeg drawtext同时添加多行文本
  3. 安装HDFS过程中Browse Directory报错
  4. java程序设计实验结论_实验报告三
  5. Android EditText 常用属性总结
  6. RK3288_Android7.1调试uart串口屏
  7. 河北省科技创新平台用例图
  8. 816D.Karen and Test 杨辉三角 规律 组合
  9. 一人身兼多个项目时的“课程表”工作模式实践
  10. BizTalk 2002:Registering Custom Components