Recently, When I improved a wcf program, I encountered a common problem: listening port number conflict. Generally, this problem occurs when multiple listeners are opened or other programs occupy the port number. So I checked according to the conventional method and thought it was easy to solve the problem. After a while, I did not find out the cause, so that I had to restart the computer. I suspect that the program is too complicated, so I simplified the program and kept only the configuration content related to the wcf, as shown below:
Original configuration file
1 <configuration>
2 <system. serviceModel>
3 <services>
4 <service name = "ComplexDataService. MyService" behaviorConfiguration = "mexConfig">
5 6 <baseAddresses>
7 <add baseAddress = "net. tcp: // localhost: 1955/MyService"/>
8 </baseAddresses>
9 10 <endpoint address = "" binding = "netTcpBinding" bindingConfiguration = "NoneSecurityAndMoreSize" contract = "ComplexDataService. IMyService"/>
11 <endpoint address = "mex" binding = "mexTcpBinding" contract = "IMetadataExchange"/>
12 </service>
13 </services>
14 <behaviors>
15 <serviceBehaviors>
16 <behavior name = "mexConfig">
17 <serviceMetadata httpGetEnabled = "true" httpGetUrl = "http: // localhost: 1956/MyService"/>
18 <serviceThrottling maxconcurrentcils = "10" maxConcurrentInstances = "10" maxConcurrentSessions = "10"/>
19 <serviceDebug includeExceptionDetailInFaults = "true"/>
20 </behavior>
21 </serviceBehaviors>
22 </behaviors>
23 <bindings>
24 <netTcpBinding>
25 <binding name = "NoneSecurityAndMoreSize" maxBufferPoolSize = "1048576" maxBufferSize = "524288" maxConnections = "20" maxcompute edmessagesize = "524288">
26 <readerQuotas maxStringContentLength = "524288" type = "regxph" text = "yourobjectname"/>
27 <security mode = "None"/>
28 </binding>
29 </netTcpBinding>
30 </bindings>
31 </system. serviceModel>
32 </configuration>
After the slimming, the problem still exists, so it can be determined that the service itself has an error. Looking at the simple exercises that I wrote when I learned about wcf, I found that none of them had this problem. The only difference is that the configuration is not so responsible. As a result, we continue to streamline the configuration and remove bindingConfiguration = "NoneSecurityAndMoreSize". The world will be harmonious and we will not report port conflict errors. Start with the configuration corresponding to NoneSecurityAndMoreSize. The problem is finally located in maxConnections = "20. After removing maxConnections, no problem will occur.
After finding the problem, you must find a solution. After a simple search, other people have encountered the same problem. Microsoft's solution is to modify the configuration so that the port number can be shared. I have heard of it before, but it is not actually used, and the solution is relatively simple. You only need to modify the configuration. The key configuration is to set portSharingEnabled in netTcpBinding to true. At the same time, for mex, custom mbmbinding is required. portSharingEnabled is supported. And apply these configurations to the corresponding endpoint.
Modified Configuration
1 <configuration>
2 <system. serviceModel>
3 <services>
4 <service name = "ComplexDataService. MyService" behaviorConfiguration = "mexConfig">
5 6 <baseAddresses>
7 <add baseAddress = "net. tcp: // localhost: 1955/MyService"/>
8 </baseAddresses>
9 10 <endpoint address = "" binding = "netTcpBinding" bindingConfiguration = "NoneSecurityAndMoreSize" contract = "ComplexDataService. IMyService"/>
11 <endpoint address = "mex"
Binding = "customBinding" bindingConfiguration = "myMexTcpBinding"Contract = "IMetadataExchange"/>
12 </service>
13 </services>
14 <behaviors>
15 <serviceBehaviors>
16 <behavior name = "mexConfig">
17 <serviceMetadata httpGetEnabled = "true" httpGetUrl = "http: // localhost: 1956/MyService"/>
18 <serviceThrottling maxconcurrentcils = "10" maxConcurrentInstances = "10" maxConcurrentSessions = "10"/>
19 <serviceDebug includeExceptionDetailInFaults = "true"/>
20 </behavior>
21 </serviceBehaviors>
22 </behaviors>
23 <bindings>
24
<CustomBinding>
25 <binding name = "myMexTcpBinding">
26 <tcpTransport portSharingEnabled = "true"/>
27 </binding>
28 </customBinding>
29 <netTcpBinding>
30 <binding name = "NoneSecurityAndMoreSize" maxBufferPoolSize = "1048576" maxBufferSize = "524288" maxConnections = "20" maxcompute edmessagesize = "524288"
PortSharingEnabled = "true">
31 <readerQuotas maxStringContentLength = "524288" type = "regxph" text = "yourobjectname"/>
32 <security mode = "None"/>
33 </binding>
34 </netTcpBinding>
35 </bindings>
36 </system. serviceModel>
37 </configuration>
I don't know why there is a problem after maxConnections is set. I need to know more.
For details, refer
Http://connect.microsoft.com/VisualStudio/feedback/details/286744/port-sharing-mextcpbinding-and-nettcpbinding-fails-when-maxconnections-or-listenbacklog-is-adjusted