在本次记录中将记录一下如果创建一个基于nettcp 或http 下的双向通讯的WCF。
tcp 下的双向通讯:
由于使用tcp的方式,会遵循tcp 的方式,即和HOstServer端先创建一个连接,然后,所有的命令都会在这个connection上进行传输,当然如果是CallBack的部分的数据也会在同一个Connection中进行传输。下面就看下如何创建一个简单的Duplex 。
以下修改 学习笔记中(1)中的程序来实现。 工程架构如下:
WCF 学习笔记(三)----构建双向通讯(tcp/http)

1.修改MyData命名空间的类。如下:
 1WCF 学习笔记(三)----构建双向通讯(tcp/http)using System;
 2WCF 学习笔记(三)----构建双向通讯(tcp/http)using System.Collections.Generic;
 3WCF 学习笔记(三)----构建双向通讯(tcp/http)using System.Text;
 4WCF 学习笔记(三)----构建双向通讯(tcp/http)using System.ServiceModel;
 5WCF 学习笔记(三)----构建双向通讯(tcp/http)
 6WCF 学习笔记(三)----构建双向通讯(tcp/http)
 7WCF 学习笔记(三)----构建双向通讯(tcp/http)namespace TESTWCF.MyData
 8}

注意方法中使用的OperationContract中要使用IsOneWay=true , 否则在调用的时候, 返回callback 后Client端会被锁住。
至于OperationContract中的属性,我也了解很少,以后再补上详细的描述。
isOneWay=true 理解为:则表明客户端与服务端之间只有请求,没有响应。
HostServer端调整:
添加一个MyDataService的类,继承IMyDataService。如下:

using System;
using System.Collections.Generic;
using System.Text;
using TESTWCF.MyData;
using System.ServiceModel;

namespace TESTWCF.HostServer
{

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class MyDataService : IMyDataService
    {
       
        #region IMyDataService 成员

        public void GetServerTime(string userName)
        {
            string str = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            ICallBack callback = OperationContext.Current.GetCallbackChannel<ICallBack>();
            callback.ServerReturn(str);

        }
        #endregion
    }
}


在这里可以看到,在GetServerTime的方法中,使用OperationContext.Current.GetCallBAckChannel的方法获取当前连接的CallBack Contact。
然后调用该契约的方法,调用Client端的方法。
Clinet端修改:
app.config :
WCF 学习笔记(三)----构建双向通讯(tcp/http)<?xml version="1.0" encoding="utf-8" ?>
WCF 学习笔记(三)----构建双向通讯(tcp/http)
<configuration>
WCF 学习笔记(三)----构建双向通讯(tcp/http)  
<system.serviceModel>
WCF 学习笔记(三)----构建双向通讯(tcp/http)      
WCF 学习笔记(三)----构建双向通讯(tcp/http)      
<client>
WCF 学习笔记(三)----构建双向通讯(tcp/http)          
<endpoint address="net.tcp://localhost:8010/MyData" binding="netTcpBinding" bindingConfiguration="" 
WCF 学习笔记(三)----构建双向通讯(tcp/http)                  contract
="TESTWCF.MyData.IMyDataService"  name="tcpendpoint" />
WCF 学习笔记(三)----构建双向通讯(tcp/http)      
<!--<endpoint address="http://localhost:8090/MyData" binding="basicHttpBinding"
WCF 学习笔记(三)----构建双向通讯(tcp/http)                contract="TESTWCF.MyData.IMyDataService" />
-->
WCF 学习笔记(三)----构建双向通讯(tcp/http)    
</client>
WCF 学习笔记(三)----构建双向通讯(tcp/http)  
</system.serviceModel>
WCF 学习笔记(三)----构建双向通讯(tcp/http)
</configuration>
窗体代码:
}
运行后即可实现 在ServerReturn中得到返回值,并显示到窗体上。


Http 下的双向通讯:

在Http的协议下,由于http协议是 一个应用层的协议,它采用传统的Request/Reply的方式进行通信,所以,当要实现双向通讯的时候,需要在Client端也建立一个Server端,用来接收CallBack的数据。
在上面的例子中,可以通过修改app.config来实现http下的双向。
Server端的app.config 修改如下:
WCF 学习笔记(三)----构建双向通讯(tcp/http)<?xml version="1.0" encoding="utf-8" ?>
WCF 学习笔记(三)----构建双向通讯(tcp/http)
<configuration>
WCF 学习笔记(三)----构建双向通讯(tcp/http)  
<system.serviceModel>
WCF 学习笔记(三)----构建双向通讯(tcp/http)    
<services>
WCF 学习笔记(三)----构建双向通讯(tcp/http)      
<service name="TESTWCF.HostServer.MyDataService" behaviorConfiguration="serviceBehavior">
WCF 学习笔记(三)----构建双向通讯(tcp/http)          
<endpoint binding="wsDualHttpBinding" contract="TESTWCF.MyData.IMyDataService" address="MyData" />
WCF 学习笔记(三)----构建双向通讯(tcp/http)          
<endpoint binding="netTcpBinding" contract="TESTWCF.MyData.IMyDataService" address="MyData" />
WCF 学习笔记(三)----构建双向通讯(tcp/http)
WCF 学习笔记(三)----构建双向通讯(tcp/http)          
<!--<endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex" />-->
WCF 学习笔记(三)----构建双向通讯(tcp/http)        
<host>
WCF 学习笔记(三)----构建双向通讯(tcp/http)          
<baseAddresses>
WCF 学习笔记(三)----构建双向通讯(tcp/http)            
<add baseAddress="http://localhost:8090"/>
WCF 学习笔记(三)----构建双向通讯(tcp/http)              
<add baseAddress="net.tcp://localhost:8010"/>
WCF 学习笔记(三)----构建双向通讯(tcp/http)
WCF 学习笔记(三)----构建双向通讯(tcp/http)          
</baseAddresses>
WCF 学习笔记(三)----构建双向通讯(tcp/http)        
</host>
WCF 学习笔记(三)----构建双向通讯(tcp/http)      
</service>
WCF 学习笔记(三)----构建双向通讯(tcp/http)    
</services>
WCF 学习笔记(三)----构建双向通讯(tcp/http)
WCF 学习笔记(三)----构建双向通讯(tcp/http)    
<behaviors>
WCF 学习笔记(三)----构建双向通讯(tcp/http)      
<serviceBehaviors>
WCF 学习笔记(三)----构建双向通讯(tcp/http)        
<behavior name="serviceBehavior">
WCF 学习笔记(三)----构建双向通讯(tcp/http)          
<serviceMetadata httpGetEnabled="false" />
WCF 学习笔记(三)----构建双向通讯(tcp/http)        
</behavior>
WCF 学习笔记(三)----构建双向通讯(tcp/http)      
</serviceBehaviors>
WCF 学习笔记(三)----构建双向通讯(tcp/http)    
</behaviors>
WCF 学习笔记(三)----构建双向通讯(tcp/http)  
</system.serviceModel>
WCF 学习笔记(三)----构建双向通讯(tcp/http)
</configuration>
Client端appconfig修改如下:
WCF 学习笔记(三)----构建双向通讯(tcp/http)<?xml version="1.0" encoding="utf-8" ?>
WCF 学习笔记(三)----构建双向通讯(tcp/http)
<configuration>
WCF 学习笔记(三)----构建双向通讯(tcp/http)  
<system.serviceModel>
WCF 学习笔记(三)----构建双向通讯(tcp/http)      
<bindings>
WCF 学习笔记(三)----构建双向通讯(tcp/http)          
<wsDualHttpBinding>
WCF 学习笔记(三)----构建双向通讯(tcp/http)              
<binding name="wsDualBinding_IMyData" clientBaseAddress="http://localhost:8888/myClient/" />
WCF 学习笔记(三)----构建双向通讯(tcp/http)          
</wsDualHttpBinding>
WCF 学习笔记(三)----构建双向通讯(tcp/http)      
</bindings>
WCF 学习笔记(三)----构建双向通讯(tcp/http)
WCF 学习笔记(三)----构建双向通讯(tcp/http)      
<client>
WCF 学习笔记(三)----构建双向通讯(tcp/http)          
<!--<endpoint address="net.tcp://localhost:8010/MyData" binding="netTcpBinding" bindingConfiguration="" 
WCF 学习笔记(三)----构建双向通讯(tcp/http)                  contract="TESTWCF.MyData.IMyDataService"  name="tcpendpoint" />
-->
WCF 学习笔记(三)----构建双向通讯(tcp/http)      
<endpoint address="http://localhost:8090/MyData" binding="wsDualHttpBinding" bindingConfiguration="wsDualBinding_IMyData"
WCF 学习笔记(三)----构建双向通讯(tcp/http)                contract
="TESTWCF.MyData.IMyDataService"  name="tcpendpoint"/>
WCF 学习笔记(三)----构建双向通讯(tcp/http)    
</client>
WCF 学习笔记(三)----构建双向通讯(tcp/http)  
</system.serviceModel>
WCF 学习笔记(三)----构建双向通讯(tcp/http)
</configuration>


通过以上修改app.config , 就可以实现一个基于http模式下的wcf 的程序了。
个人觉得是用tcp的方式要比http的方式要好,因为tcp的方式,使用二进制的方式进行网络传输, 而http的方式使用文本的方式进行传输。tcp使用tcp的方式建立连接,保证数据不丢,而且tcp的方式是先建立一个连接,然后再该连接上进行操作  , 所以效能上也比http的方式要快。


源码下载




相关文章: