首页 > 学院 > 开发设计 > 正文

权限管理系统系列之WCF通信

2019-11-17 02:43:50
字体:
来源:转载
供稿:网友

权限管理系统系列之WCF通信

目录

权限管理系统系列之序言

首先说下题外话,有些园友看了前一篇【权限管理系统系列之序言】博客加了QQ群(186841119),看了我写的权限管理系统的相关文档(主要是介绍已经开发的功能),给出了一些建议,感觉非常好,希望后续有更多的园友能再接再厉给出更多的指导意见,在平常的开发中个人会结合你们的建议做出适当修改和完善,促进共同学习和进步。关于源码共享的问题,可能会过段时间公布,不会现在公开源码,个人还在不断完善中,等完成差不多后会公开源码。

客户端与服务器的通信在一个程序中会占住关键的作用,处理起来可能会有很多方式,比如说Remoting、Socket、WebServices、WCF等等都可以实现。本人这几种基本上都用过,Socket可能比较少些,一些聊天室的程序就会使用Socket,通过字节的形式接收数据;WebServices会WinCE开发中使用到,数据传输进行压缩,这样操作数据就比较方便,实时操作数据库;Remoting主要用在MIS系统的客户端与服务端通信,个人也说不出那种好;WCF也是我最近一两年才接触到的,公司现在使用的就是WCF通信的,个人感觉用WCF比较方便和简单,实用起来使用三个函数(一个函数是检测客户端与服务器端的心跳,一个是用于登录的、一个是公共的接口,基本上所有的客户端和服务端的通信都是用這个函数),這个函数可以搞定所有的客户端访问服务端的方法,所有的SQL在服务端执行,便于维护和日常的分工,不过在平常的开发中也不会分客户端和服务端的开发,基本上也是一个一个模块进行分工的。

WCF的配置(包括客户端和服务端)

客户端的配置文件:

 1 <?xml version="1.0"?> 2 <configuration> 3   <system.serviceModel> 4     <bindings> 5       <netTcpBinding> 6         <binding name="TcpBinding_AppService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" transactionFlow="false" transferMode="Buffered" transactionPRotocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="10485760" maxBufferSize="10485760" maxConnections="10" maxReceivedMessageSize="10485760"> 7           <readerQuotas maxDepth="32" maxStringContentLength="10485760" maxArrayLength="10485760" maxBytesPerRead="10485760" maxNameTableCharCount="10485760"/> 8           <reliablesession ordered="true" inactivityTimeout="00:10:00" enabled="false"/> 9           <security mode="None">10             <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign"/>11             <message clientCredentialType="Windows"/>12           </security>13         </binding>14 15         <binding name="TcpBinding_MessageService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="10485760" maxBufferSize="10485760" maxConnections="10" maxReceivedMessageSize="10485760">16           <readerQuotas maxDepth="32" maxStringContentLength="10485760" maxArrayLength="10485760" maxBytesPerRead="10485760" maxNameTableCharCount="10485760"/>17           <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"/>18           <security mode="None">19             <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign"/>20             <message clientCredentialType="Windows"/>21           </security>22         </binding>23 24       </netTcpBinding>25     </bindings>26     <client>27       <endpoint address="net.tcp://localhost:9090/AppService" binding="netTcpBinding" bindingConfiguration="TcpBinding_AppService" contract="IAppService" name="TcpBinding_AppService"/>28 29       <endpoint address="net.tcp://localhost:7070/MessageService" binding="netTcpBinding" bindingConfiguration="TcpBinding_MessageService" contract="IMessageService" name="TcpBinding_MessageService"/>30     </client>31   </system.serviceModel>32 </configuration>
View Code

服务端的配置文件:

 1 <?xml version="1.0"?> 2 <configuration> 3   <system.serviceModel> 4     <services> 5       <service behaviorConfiguration="Service.Behavior" name="Server.AppService"> 6         <endpoint address="AppService" binding="netTcpBinding" bindingConfiguration="AppServiceBinding" name="TcpBinding_AppService" contract="Server.IAppService" /> 7         <endpoint address="AppService/mex" binding="mexTcpBinding" contract="IMetadataExchange" /> 8         <host> 9           <baseAddresses>10             <add baseAddress="net.tcp://localhost:9090" />11           </baseAddresses>12         </host>13       </service>14       <service behaviorConfiguration="Service.Behavior" name="Server.MessageService">15         <endpoint address="MessageService" binding="netTcpBinding" bindingConfiguration="MessageServiceBinding" name="TcpBinding_MessageService" contract="Server.IMessageService" />16         <endpoint address="MessageService/mex" binding="mexTcpBinding" contract="IMetadataExchange" />17         <host>18           <baseAddresses>19             <add baseAddress="net.tcp://localhost:7070" />20           </baseAddresses>21         </host>22       </service>23     </services>24     <bindings>25       <netTcpBinding>26         <binding name="AppServiceBinding" maxBufferSize="10485760" maxReceivedMessageSize="10485760">27           <readerQuotas maxDepth="32" maxStringContentLength="10485760"28             maxArrayLength="10485760" maxBytesPerRead="10485760" maxNameTableCharCount="10485760" />29           <reliableSession ordered="true" inactivityTimeout="00:10:00"30             enabled="false" />31           <security mode="None" />32         </binding>33         <binding name="MessageServiceBinding" maxBufferSize="10485760" maxReceivedMessageSize="10485760">34           <readerQuotas maxDepth="32" maxStringContentLength="10485760"35             maxArrayLength="10485760" maxBytesPerRead="10485760" maxNameTableCharCount="10485760" />36           <reliableSession ordered="true" inactivityTimeout="00:10:00"37             enabled="false" />38           <security mode="None" />39         </binding>40       </netTcpBinding>41     </bindings>42     <behaviors>43       <serviceBehaviors>44         <behavior name="Service.Behavior">45           <serviceMetadata />46           <serviceDebug includeExceptionDetailInFaults="true" />47           <!--会话最大数量(并发会话)-->48           <serviceThrottling maxConcurrentSessions="100" />49           <!--数据序列最大量-->50           <dataContractSerializer maxItemsInObjectGraph="10485760" />51         </behavior>52         <behavior name="mexConfig">53           <serviceDebug includeExceptionDetailInFaults="True" />54           <serviceMetadata />55         </behavior>56       </serviceBehaviors>57     </behaviors>58   </system.serviceModel>59 </configuration>
View Code

以上为客户端与服务端的配置文件,有两个配置,一个为基本通信所用,一个为双工通信所用。

介绍完配置文件后再介绍实现函数:

 1 [ServiceContract(Name = "IAppService", SessionMode = SessionMode.Allowed, Namespace = "http://tempuri.org/")] 2     public interface IAppService 3     { 4         //心跳 5         [OperationContract] 6         string HeartBeat(string echo); 7  8         //登录 9         [OperationContract]10         bool Login(string UserName, string PassWord);11 12         //统一的应用业务请求调用,用于会话控制和调用转发13         [OperationContract]14         Result AppCall(Request request);15     }16 17     #region * 推送消息18     [ServiceContract(CallbackContract = typeof(ipushClient))]19     public interface IMessageService20     {21         [OperationContract]22         void RegisterClient();23     }24 25     pu
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表