WCF配置詳解


服務端的配置文件主要是對services、bindings、behaviors的配置。在默認的App.config中,使用的是WCF Framework定義好的wsHttpBinding默認配置,所以看不到binding配置節。

 

配置節展開如下圖:

 

 

BTW: "元數據端點”通過WS-MetadataExchange幫我們實現了對服務的描述,提供了WSDL,啟動Host之后我們可以通過<http://localhost:8732/Design_Time_Addresses/WcfServiceLib/Service1/?wsdl> 查看到公開的服務描述。

配置節展開如下圖:

  

 

 

 

關於WCF中的地址和綁定,需要補充一下。

WCF中支持的傳輸協議包括HTTP、TCP、Peer network(對等網)、IPC(基於命名管道的內部進程通信)以及MSMQ(微軟消息隊列),每個協議對應一個地址類型:

  • HTTP地址:<http://localhost:8080/>
  • TCP地址: net.tcp://localhost:8080/
  • IPC地址: net.pipe://localhost/  (適用於跨進程,不能使用於不同機器間)
  • MSMQ地址: net.msmq://localhost/
  • 對等網地址: net.p2p://localhost/

WCF中提供的綁定有:

  • BasicHttpBinding: 最簡單的綁定類型,通常用於 Web Services。使用 HTTP 協議,Text/XML 編碼方式。
  • WSHttpBinding: 比 BasicHttpBinding 更加安全,通常用於 non-duplex 服務通訊。
  • WSDualHttpBinding: 和 WSHttpBinding 相比,它支持 duplex 類型的服務。
  • WSFederationHttpBinding: 支持 WS-Federation 安全通訊協議。
  • NetTcpBinding: 效率最高,安全的跨機器通訊方式。
  • NetNamedPipeBinding: 安全、可靠、高效的單機服務通訊方式。
  • NetMsmqBinding: 使用消息隊列在不同機器間進行通訊。
  • NetPeerTcpBinding: 使用 P2P 協議在多機器間通訊。
  • MsmqIntegrationBinding: 使用現有的消息隊列系統進行跨機器通訊。如 MSMQ。

 

 

OK,有了上面的基礎,就讓WCF風暴來的猛烈些吧。做一個多服務,多端點的示例。

1.WcfServiceLib 代碼:

[ServiceContract] 

   publicinterfaceIService 

  

       [OperationContract] 

       stringGetMessage(); 

  

   publicclassService1 : IService 

  

       publicstringGetMessage() 

       { 

           var address = OperationContext.Current.Channel.LocalAddress.ToString(); 

           returnstring.Format("From Server1: Hello Client at [{0}]", address);  

       } 

  

   publicclassService2 : IService 

  

       publicstringGetMessage() 

       { 

           var address = OperationContext.Current.Channel.LocalAddress.ToString(); 

           returnstring.Format("來自Service2: Client at [{0}]", address); 

       } 

  

2.WcfConsoleHost 代碼:

   staticvoidMain(string[] args) 

  

       ServiceHost host1 = newServiceHost(typeof(WcfServiceLib.Service1)); 

       host1.Open(); 

       Console.WriteLine("Server1 Opened!"); 

       ServiceHost host2 = newServiceHost(typeof(WcfServiceLib.Service2)); 

       host2.Open(); 

       Console.WriteLine("Server2 Opened!"); 

       Console.Read(); 

  

3.服務端配置文件:

   <?xmlversion="1.0"encoding="utf-8"?> 

   <configuration> 

     <system.web> 

       <compilationdebug="true"/> 

     </system.web> 

     <system.serviceModel> 

       <services> 

         <servicename="WcfServiceLib.Service1"> 

           <host> 

             <baseAddresses> 

               <addbaseAddress= "http://localhost:9999/WcfStudy3/Service1"/> 

               <addbaseAddress= "net.tcp://localhost:8888/WcfStudy3/Service1"/> 

             </baseAddresses> 

           </host> 

           <endpointaddress="serviceEN_1"binding="wsHttpBinding"contract="WcfServiceLib.IService"/> 

           <endpointaddress="serviceEN_2"binding="mexTcpBinding"contract="WcfServiceLib.IService"/> 

           <endpointaddress="mex"binding="mexHttpBinding"contract="IMetadataExchange"/> 

         </service> 

         <servicename="WcfServiceLib.Service2"> 

           <host> 

             <baseAddresses> 

               <addbaseAddress= "http://localhost:9999/WcfStudy3/Service2"/> 

               <addbaseAddress= "net.tcp://localhost:8888/WcfStudy3/Service2"/> 

             </baseAddresses> 

           </host> 

           <endpointaddress="serviceCH_1"binding="wsHttpBinding"contract="WcfServiceLib.IService"/> 

           <endpointaddress="serviceCH_2"binding="mexTcpBinding"contract="WcfServiceLib.IService"/> 

           <endpointaddress="mex"binding="mexHttpBinding"contract="IMetadataExchange"/> 

         </service> 

       </services> 

       <behaviors> 

         <serviceBehaviors> 

           <behavior> 

             <serviceMetadatahttpGetEnabled="True"/> 

             <serviceDebugincludeExceptionDetailInFaults="true"/> 

           </behavior> 

         </serviceBehaviors> 

       </behaviors> 

     </system.serviceModel> 

   </configuration> 

4. 啟動Host,在Client工程中添加Service Reference
因為有兩個Service,所以要添加兩次。
(1) WcfSvc1(Url:http://localhost:9999/WcfStudy3/Service1

 

(2) WcfSvc2(Url:<http://localhost:9999/WcfStudy3/Service2>) 圖略

5. 客戶端配置文件: 配置節中,生成了4個Endpoint,分別對應服務端的4個Endpoint。通過
name屬性區別。

   <client> 

       <endpoint address="http://localhost:9999/WcfStudy3/Service1/serviceEN_1" 

           binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService" 

           contract="WcfSvc1.IService" name="WSHttpBinding_IService"> 

       </endpoint> 

       <endpoint address="net.tcp://localhost:8888/WcfStudy3/Service1/serviceEN_2" 

           binding="netTcpBinding" bindingConfiguration="MetadataExchangeTcpBinding_IService" 

           contract="WcfSvc1.IService" name="MetadataExchangeTcpBinding_IService" /> 

       <endpoint address="http://localhost:9999/WcfStudy3/Service2/serviceCH_1" 

           binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1" 

           contract="WcfSvc2.IService" name="WSHttpBinding_IService1"> 

       </endpoint> 

       <endpoint address="net.tcp://localhost:8888/WcfStudy3/Service2/serviceCH_2" 

           binding="netTcpBinding" bindingConfiguration="MetadataExchangeTcpBinding_IService1" 

           contract="WcfSvc2.IService" name="MetadataExchangeTcpBinding_IService1" /> 

   </client> 

6. 客戶端代碼:

   staticvoidMain(string[] args) 

  

       Console.WriteLine("------------"); 

       WcfSvc1.ServiceClient client1_1 = newWcfSvc1.ServiceClient("WSHttpBinding_IService"); 

       Console.WriteLine(client1_1.GetMessage()); 

       Console.WriteLine("------------"); 

       WcfSvc1.ServiceClient client1_2 = newWcfSvc1.ServiceClient("MetadataExchangeTcpBinding_IService"); 

       Console.WriteLine(client1_2.GetMessage()); 

       Console.WriteLine("------------"); 

       WcfSvc2.ServiceClient client2_1 = newWcfSvc2.ServiceClient("WSHttpBinding_IService1"); 

       Console.WriteLine(client2_1.GetMessage()); 

       Console.WriteLine("------------"); 

       WcfSvc2.ServiceClient client2_2 = newWcfSvc2.ServiceClient("MetadataExchangeTcpBinding_IService1"); 

       Console.WriteLine(client2_2.GetMessage()); 

       Console.Read(); 

  

7.運行結果:

 

有人會問,那么生成完的配置文件都要一個個手動修改嗎?答案當然不是,VS已經為我們准備了WCF配置工具:IDE > Tools > WCF Service Configuration Editor 。

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM