目錄
權限管理系統系列之序言
首先說下題外話,有些園友看了前一篇【權限管理系統系列之序言】博客加了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" transactionsession 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
新聞熱點
疑難解答