博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
化零为整WCF(6) - 消息处理(异步调用OneWay, 双向通讯Duplex)
阅读量:5900 次
发布时间:2019-06-19

本文共 7642 字,大约阅读时间需要 25 分钟。


 



化零为整WCF(6) - 消息处理(异步调用OneWay, 双向通讯Duplex)


作者:



介绍

WCF(Windows Communication Foundation) - 消息处理:通过操作契约的
IsOneWay参数实现异步调用,基于
Http, TCP, Named Pipe, MSMQ的双向通讯。
示例(异步调用OneWay)
1、服务
IOneWay.cs
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Linq; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
using System.ServiceModel; 

InBlock.gif 

InBlock.gif
namespace WCF.ServiceLib.Message 

InBlock.gif

InBlock.gif     
/// <summary> 

InBlock.gif        
/// IOneWay接口 

InBlock.gif        
/// </summary> 

InBlock.gif        [ServiceContract] 

InBlock.gif        
public 
interface IOneWay 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 不使用OneWay(同步调用) 

InBlock.gif                
/// </summary> 

InBlock.gif                [OperationContract] 

InBlock.gif                
void WithoutOneWay(); 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 使用OneWay(异步调用) 

InBlock.gif                
/// </summary> 

InBlock.gif                [OperationContract(IsOneWay=
true)] 

InBlock.gif                
void WithOneWay(); 

InBlock.gif        } 

InBlock.gif}
 
OneWay.cs
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Linq; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
using System.ServiceModel; 

InBlock.gif 

InBlock.gif
namespace WCF.ServiceLib.Message 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// OneWay类 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class OneWay : IOneWay 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 不使用OneWay(同步调用) 

InBlock.gif                
/// 抛出Exception异常 

InBlock.gif                
/// </summary> 

InBlock.gif                
public 
void WithoutOneWay() 

InBlock.gif                { 

InBlock.gif                        
throw 
new System.Exception(
"抛出Exception异常"); 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 使用OneWay(异步调用) 

InBlock.gif                
/// 抛出Exception异常 

InBlock.gif                
/// </summary> 

InBlock.gif                
public 
void WithOneWay() 

InBlock.gif                { 

InBlock.gif                        
throw 
new System.Exception(
"抛出Exception异常"); 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
 
2、宿主
OneWay.cs
InBlock.gif
using (ServiceHost host = 
new ServiceHost(
typeof(WCF.ServiceLib.Message.OneWay))) 

InBlock.gif

InBlock.gif        host.Open(); 

InBlock.gif 

InBlock.gif        Console.WriteLine(
"服务已启动(WCF.ServiceLib.Message.OneWay)"); 

InBlock.gif        Console.WriteLine(
"按<ENTER>停止服务"); 

InBlock.gif        Console.ReadLine(); 

InBlock.gif 

InBlock.gif}
 
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、客户端
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Linq; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
using System.Windows.Forms; 

InBlock.gif
using System.ServiceModel; 

InBlock.gif 

InBlock.gif
namespace Client2.Message 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// 演示Message.OneWay的类 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class OneWay 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 调用IsOneWay=true的操作契约(异步操作) 

InBlock.gif                
/// </summary> 

InBlock.gif                
public 
void HelloWithOneWay() 

InBlock.gif                { 

InBlock.gif                        
try 

InBlock.gif                        { 

InBlock.gif                                var proxy = 
new MessageSvc.OneWay.OneWayClient(); 

InBlock.gif 

InBlock.gif                                proxy.WithOneWay(); 

InBlock.gif 

InBlock.gif                                proxy.Close(); 

InBlock.gif                        } 

InBlock.gif                        
catch (Exception ex) 

InBlock.gif                        { 

InBlock.gif                                MessageBox.Show(ex.ToString()); 

InBlock.gif                        } 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 调用IsOneWay=false的操作契约(同步操作) 

InBlock.gif                
/// </summary> 

InBlock.gif                
public 
void HelloWithoutOneWay() 

InBlock.gif                { 

InBlock.gif                        
try 

InBlock.gif                        { 

InBlock.gif                                var proxy = 
new MessageSvc.OneWay.OneWayClient(); 

InBlock.gif 

InBlock.gif                                proxy.WithoutOneWay(); 

InBlock.gif 

InBlock.gif                                proxy.Close(); 

InBlock.gif                        } 

InBlock.gif                        
catch (Exception ex) 

InBlock.gif                        { 

InBlock.gif                                MessageBox.Show(ex.ToString()); 

InBlock.gif                        } 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
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
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Linq; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
using System.ServiceModel; 

InBlock.gif 

InBlock.gif
namespace WCF.ServiceLib.Message 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// IDuplex接口 

InBlock.gif        
/// </summary> 

InBlock.gif        
/// <remarks> 
InBlock.gif        /// CallbackContract - 回调接口 
InBlock.gif        /// </remarks> 

InBlock.gif        [ServiceContract(CallbackContract = 
typeof(IDuplexCallback))] 

InBlock.gif        
public 
interface IDuplex 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// Hello 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="name">名字</param> 

InBlock.gif                [OperationContract(IsOneWay = 
true)] 

InBlock.gif                
void HelloDuplex(
string name); 

InBlock.gif        } 

InBlock.gif 

InBlock.gif        
/// <summary> 

InBlock.gif        
/// IDuplex回调接口 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
interface IDuplexCallback 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// Hello 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="name"></param> 

InBlock.gif                [OperationContract(IsOneWay = 
true)] 

InBlock.gif                
void HelloDuplexCallback(
string name); 

InBlock.gif        } 

InBlock.gif}
 
Duplex.cs
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Linq; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
using System.ServiceModel; 

InBlock.gif 

InBlock.gif
namespace WCF.ServiceLib.Message 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// Duplex类 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class Duplex : IDuplex 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// Hello 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="name">名字</param> 

InBlock.gif                
public 
void HelloDuplex(
string name) 

InBlock.gif                { 

InBlock.gif                        
// 声明回调接口 

InBlock.gif                        IDuplexCallback callback = OperationContext.Current.GetCallbackChannel<IDuplexCallback>(); 

InBlock.gif                         

InBlock.gif                        
// 调用回调接口中的方法 

InBlock.gif                        callback.HelloDuplexCallback(name); 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
 
2、 宿主
Duplex.cs
InBlock.gif
using (ServiceHost host = 
new ServiceHost(
typeof(WCF.ServiceLib.Message.Duplex))) 

InBlock.gif

InBlock.gif        host.Open(); 

InBlock.gif 

InBlock.gif        Console.WriteLine(
"服务已启动(WCF.ServiceLib.Message.Duplex)"); 

InBlock.gif        Console.WriteLine(
"按<ENTER>停止服务"); 

InBlock.gif        Console.ReadLine(); 

InBlock.gif 

InBlock.gif}
 
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
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Linq; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
using System.ServiceModel; 

InBlock.gif
using System.Windows.Forms; 

InBlock.gif 

InBlock.gif
namespace Client2.Message 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// 演示Message.Duplex的类 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class Duplex 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// Hello 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="name">名字</param> 

InBlock.gif                
public 
void HelloDulex(
string name) 

InBlock.gif                { 

InBlock.gif                        var ct = 
new Client2.Message.CallbackType(); 

InBlock.gif                        var ctx = 
new InstanceContext(ct); 

InBlock.gif 

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

InBlock.gif 

InBlock.gif                        proxy.HelloDuplex(name); 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
CallbackType.cs
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Linq; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
using System.Windows.Forms; 

InBlock.gif 

InBlock.gif
namespace Client2.Message 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// 实现回调接口 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class CallbackType : MessageSvc.Duplex.IDuplexCallback 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// Hello 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="name">名字</param> 

InBlock.gif                
public 
void HelloDuplexCallback(
string name) 

InBlock.gif                { 

InBlock.gif                        MessageBox.Show(
"Hello: " + name); 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
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"
OK
     本文转自webabcd 51CTO博客,原文链接:
http://blog.51cto.com/webabcd/344116
,如需转载请自行联系原作者
你可能感兴趣的文章
flash back mysql_mysqlbinlog flashback 使用最佳实践
查看>>
mysql存储引擎模式_MySQL存储引擎
查看>>
python类 del_全面了解Python类的内置方法
查看>>
java jni 原理_使用JNI技术实现Java和C++的交互
查看>>
java 重写system.out_重写System.out.println(String x)方法
查看>>
配置ORACLE 11g绿色版客户端和PLSQL远程连接环境
查看>>
ASP.NET中 DataList(数据列表)的使用前台绑定
查看>>
Linux学习之CentOS(八)--Linux系统的分区概念
查看>>
System.Func<>与System.Action<>
查看>>
asp.net开源CMS推荐
查看>>
csharp skype send message in winform
查看>>
MMORPG 游戏服务器端设计--转载
查看>>
SILK 的 Tilt的意思
查看>>
Html学习笔记3
查看>>
HDFS dfsclient写文件过程 源码分析
查看>>
ubuntu下安装libxml2
查看>>
nginx_lua_waf安装测试
查看>>
WinForm窗体缩放动画
查看>>
JQuery入门(2)
查看>>
linux文件描述符
查看>>