Now it's a little early in the morning. I have prepared the Port Sharing content, but because it's too late, I have to go to work tomorrow, so we're not going to take a long time to explain it, let everyone understand.
Today, let's talk about Port Sharing. What is Port Sharing? In wcf, Port Sharing is actually a service address of http: // 127.0.0.1: 80/calService, and another service address of http: 127.0.0.1: 80/weatherService, however, the ports are the same, which cannot be run in wcf. After the first service is started, an exception will occur if the second service is to be started. To illustrate the Port Sharing of wcf, we will give a simple example.
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>
In the configuration file, we use two console applications to start each service separately, but our configuration file uses the same content. As you can see, the port addresses of both services are 8080.
Now let's explain that the first console host, the second is the same as the first one, but the service type has changed.
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. consoleHost10 {11 class Program12 {13 static void Main (string [] args) 14 {15 using (ServiceHost guoHost = new ServiceHost (typeof (GuoService ))) 16 {17 18 if (guoHost. descriptio N. 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 + = delegate26 {27 Console. writeLine ("guoService started successfully"); 28}; 29 try30 {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}
We start the two console host programs respectively, using
By starting a new instance, we can debug the two console host programs separately.
The configuration file <binding name = "portSharing" portSharingEnabled = "true"> </binding> portSharingEnabled must be changed to false.
If the expected result is displayed, the abnormal information of the address already exists, that is, the port is occupied. This is the problem to be solved today.
We can solve this port problem by changing portSharingEnabled to true.
Of course, this problem is for the netTcp protocol. I have found several minor problems:
- PortSharingEnabled is set to true, as long as the first started Host console is set to true, and the second is set to false, it does not affect the program's success.
- To achieve Port Sharing over tcp, you must enable the netTcp service. However, if this service is not enabled on my local computer, Port Sharing can be implemented normally, I don't know whether it's a technological improvement or whether this service is just a decoration.
After finishing the tcp protocol, let's talk about the http protocol. As long as the access protocol of the configuration file is changed to http, the http protocol can be converted. I have to say that the wcf design is really powerful.
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>
Note the following two points:
- Port I set port 80, which is the default port of iis, and my iis version is 5.1
- The http protocol is used, and the port numbers of the two services are the same.
Let's see what will happen. I 'd like to repeat it. My iis version is 5.1, not iis 6 or iis 7.
It is also the exception information occupied by the address, but this exception information is not caused by the application, because this error occurs when we start the first host Program, this exception information is caused by iis, because iis 5 excludes port 80 and our program port is also 80, which causes the exception information occupied by this address. If iis 6 or iis 7 is used, this exception is not reported because the underlying implementation mechanism is different.
If you want to solve Port Sharing of port 80 under iis 5 over http, I have not found a solution until the program port is changed.
Well, today's topic is port sharing. This is a real problem. We often encounter this problem inadvertently during daily development, it is helpful for us to solve the problem.
Make an inspirational speech: Today I do not work hard, tomorrow's head is empty, others open a BMW, I cry at home.
I hope you will not have such a result.