現在時間已經是凌晨一點了,我准備了端口共享的內容,但是因為時間太晚,明天還要上班,所以我們就不長篇大徐了,剪短的說明一下內容,讓大家明白就可以了。
今天來說一下端口共享,什么是端口共享呢?在wcf中,所謂的端口共享其實就是一個服務的地址為http://127.0.0.1:80/calService,而另一個服務的地址也為http:127.0.0.1:80/weatherService,但是端口是一樣的,在wcf中這其實是不能運行的。第一個服務啟動以后,第二個服務如果要啟動的話就會出現異常,為了說明wcf的端口共享,我們仍然是來舉個簡單的例子說明一下。
1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3 <system.serviceModel>
4 <services>
5 <service name="Chinaer.WcfDemo.Services.GuoService">
6 <endpoint address="net.tcp://127.0.0.1:8080/guoService" bindingConfiguration="portSharing" binding="netTcpBinding" contract="Chinaer.WcfDemo.Contracts.IGuo"></endpoint>
7 </service>
8 <service name="Chinaer.WcfDemo.Services.GuoTwoService">
9 <endpoint address="net.tcp://127.0.0.1:8080/guoTwoService" bindingConfiguration="portSharing" binding="netTcpBinding" contract="Chinaer.WcfDemo.Contracts.IGuoTwo"></endpoint>
10 </service>
11 </services>
12
13 <behaviors></behaviors>
14
15 <bindings>
16 <netTcpBinding>
17 <binding name="portSharing" portSharingEnabled="true"></binding>
18 </netTcpBinding>
19
20 </bindings>
21
22 </system.serviceModel>
23
24
25 </configuration>
在配置文件中,我們采用兩個控制台應用程序來分別啟動每個服務,但是我們的配置文件都采用同樣的內容。大家可以看到,這兩個服務的端口地址都是8080.
現在我們來說明第一個控制台宿主,第二個和第一個一致,只是服務類型更改了而已。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.ServiceModel; 6 using Chinaer.WcfDemo.Services; 7 using System.ServiceModel.Description; 8
9 namespace Chinaer.WcfDemo.ConsoleHost 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 using (ServiceHost guoHost = new ServiceHost(typeof(GuoService))) 16 { 17
18 if (guoHost.Description.Behaviors.Find<ServiceMetadataBehavior>() == null) 19 { 20 ServiceMetadataBehavior metaDataBehavior = new ServiceMetadataBehavior(); 21 metaDataBehavior.HttpGetEnabled = true; 22 metaDataBehavior.HttpGetUrl = new Uri("http://127.0.0.1:8088/guoService/mex"); 23 guoHost.Description.Behaviors.Add(metaDataBehavior); 24 } 25 guoHost.Opened += delegate
26 { 27 Console.WriteLine("guoService 啟動成功"); 28 }; 29 try
30 { 31 if (guoHost.State != CommunicationState.Opened) 32 { 33 guoHost.Open(); 34 Console.Read(); 35 } 36 } 37 catch (CommunicationException ex) 38 { 39 guoHost.Abort(); 40 } 41 catch (Exception ex) 42 { 43 guoHost.Abort(); 44 } 45 } 46 Console.Read(); 47 } 48 } 49 }
我們分別啟動這兩個控制台宿主程序,采用
采用啟動新實例的方式我們可以分別調試這兩個控制台宿主程序。
首先有一點要說明一下,就是配置文件中 <binding name="portSharing" portSharingEnabled="true"></binding> portSharingEnabled要更改為false。
出現了我們想要的結果,地址已經存在的異常信息,就是端口已經被占用,這就是我們今天要解決的問題。
我們把portSharingEnabled要更改為true 就可以解決這個端口的問題。
當然這個問題是針對的netTcp協議,我發現了幾個小問題:
- portSharingEnabled設置為true,只要第一個啟動的宿主控制台設置為true就可以,第二個設置為false,也不影響程序的成功。
- 網上說tcp要實現端口共享,必須要開啟netTcp 服務,但是我在我本機上沒有開啟這個服務也是可以正常的實現端口共享,不知道是技術進步了還是這個服務只是一個擺設。
說完了tcp協議,現在我們來說一下http協議,只要把配置文件的訪問協議更改為http就可以實現http協議的轉換,不得不說wcf的設計確實很強大。
1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3 <system.serviceModel>
4 <services>
5 <service name="Chinaer.WcfDemo.Services.GuoService">
6 <endpoint address="http://127.0.0.1:80/guoService" binding="wsHttpBinding" contract="Chinaer.WcfDemo.Contracts.IGuo"></endpoint>
7 </service>
8 <service name="Chinaer.WcfDemo.Services.GuoTwoService">
9 <endpoint address="http://127.0.0.1:80/guoTwoService" binding="wsHttpBinding" contract="Chinaer.WcfDemo.Contracts.IGuoTwo"></endpoint>
10 </service>
11 </services>
12
13 <behaviors></behaviors>
14
15 <bindings>
16 <netTcpBinding>
17 <binding name="portSharing" portSharingEnabled="false"></binding>
18 </netTcpBinding>
19
20 </bindings>
21
22 </system.serviceModel>
23
24
25 </configuration>
請注意兩點:
- 端口我設置的端口是80,這是iis的默認端口,並且我的iis版本是5.1
- 采用http協議,兩個服務的端口號也相同
我們來看看會發生什么吧,我還是要重復一下,我的iis版本是5.1,而不是iis 6或iis 7.
也是那個地址被占用的異常信息,但是這次這個異常信息不是由應用程序引起的,因為我們啟動第一個宿主程序的時候就出現了這個錯誤,這個異常信息是由iis引起的,因為iis 5 會獨占80端口,我們的程序端口也是80,所以就引起了這個地址已被占用的異常信息。如果使用的是iis 6或iis 7,因為底層的實現機制不同,所以不會出現這個異常信息。
如果要解決iis 5下80端口在http協議下的端口共享,我至今沒有找到解決方案,除非更改程序的端口以外。
好了,今天的主題就是端口共享問題,這是一個實際的問題所在,我們在日常開發中會經常不經意間遇到他,知道了問題所在,對於我們解決問題是會很有幫助。
說個勵志話:今天不努力,明天腦袋空,別人開寶馬,我在家中哭。
但願你不會有這樣的結果。