消息处理(异步调用OneWay, 双向通讯Duplex)
2012-05-03 00:12
316 查看
消息处理(异步调用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
OneWay.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCF.ServiceLib.Message
2、宿主
OneWay.cs
using (ServiceHost host = new ServiceHost(typeof(WCF.ServiceLib.Message.OneWay)))
<?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
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
Duplex.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCF.ServiceLib.Message
2、宿主
Duplex.cs
using (ServiceHost host = new ServiceHost(typeof(WCF.ServiceLib.Message.Duplex)))
<?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
CallbackType.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Client2.Message
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"
转自 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
OneWay.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCF.ServiceLib.Message
2、宿主
OneWay.cs
using (ServiceHost host = new ServiceHost(typeof(WCF.ServiceLib.Message.OneWay)))
<?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
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
Duplex.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCF.ServiceLib.Message
2、宿主
Duplex.cs
using (ServiceHost host = new ServiceHost(typeof(WCF.ServiceLib.Message.Duplex)))
<?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
CallbackType.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Client2.Message
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"
相关文章推荐
- 化零为整WCF(6) - 消息处理(异步调用OneWay, 双向通讯Duplex)
- 化零为整WCF(6) - 消息处理(异步调用OneWay, 双向通讯Duplex)
- 化零为整WCF(6) - 消息处理(异步调用OneWay, 双向通讯Duplex)
- Android异步通讯范例(异步消息处理) 一
- iphone 消息处理事件 postNotificationName和addObserver的调用
- Android异步消息处理机制完全解析,带你从源码的角度彻底理解
- 系出名门Android(10) - HTTP 通信, XML 解析, 通过 Hander 实现异步消息处理
- [学习总结]6、Android异步消息处理机制完全解析,带你从源码的角度彻底理解
- Android 的异步消息处理机制
- Android——异步消息处理之Handler
- 命令模式下的异步消息处理(Handler,Message,Looper,Thread)
- Android Handler 异步消息处理机制的妙用 创建强大的图片加载类
- Android串口通讯,在线程里一直监听是否有消息发来,接收到消息到runOnUiThread处理跳转页面,页面跳转关闭之后,在发送消息尽然还会执行在打开页面?
- Android异步消息处理线程之----Looper+MessageQueue+Handler
- Android 异步消息处理机制 Looper、Handler、Message三者关系总结
- LIFO和FIFO——Handler 异步消息处理机制的妙用 创建强大的图片加载类
- Android 异步消息处理机制
- Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系
- 使用Spring JMS简化异步消息处理
- 解析Handler异步消息处理!