WCF异常:HTTP 无法注册,另一应用程序正在使用 TCP 端口 80

今天,调试服务的时候,忽然抛了个异常。异常信息是:"HTTP 无法注册 URL http://+/Temporary_Listen_Addresses/144ff7cb-10a4-4836-b76a-1a516da4ebda/,因为另一应用程序正在使用 TCP 端口 80。"

原来,主要是因为默认80端口已经被其他程序占用。所以WCF服务在设置默认的绑定结点时会抛异常。

解决方法如下:
在客户端代码中设置绑定的地址。代码如下:

 InstanceContext instanceContext = new InstanceContext(new CallbackHandler());
 var client 
= new ServiceReference2.CalculatorClient(instanceContext);
 WSDualHttpBinding ws 
= (WSDualHttpBinding)client.Endpoint.Binding;
 ws.ClientBaseAddress 
= new Uri(http://localhost:30001/);//设定绑定的地址
 Console.WriteLine(
"Press <ENTER> to terminate client once the output is displayed.");
 Console.WriteLine();
 
double value1 = 100.00D;
 client.AddTo(value1);
 client.Clear();
 Console.ReadLine();
 client.Close();

此外,还可以通过更改客户端的服务配置文件,设置绑定的地址来解决。如下:

 <bindings>
      
<wsDualHttpBinding>
        
<binding name="WSDualHttpBinding_ICalculator" closeTimeout="00:01:00" clientBaseAddress="http://localhost:30001/"
            openTimeout
="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
            bypassProxyOnLocal
="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
            maxBufferPoolSize
="524288" maxReceivedMessageSize="65536"
            messageEncoding
="Text" textEncoding="utf-8" useDefaultWebProxy="true">
          
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead
="4096" maxNameTableCharCount="16384" />
          
<reliableSession ordered="true" inactivityTimeout="00:10:00" />
          
<security mode="Message">
            
<message clientCredentialType="Windows" negotiateServiceCredential="true"
                algorithmSuite
="Default" />
          
</security>
        
</binding>
      
</wsDualHttpBinding>
 
</bindings>

这样,服务就可以正常了。

posted @ 2010-01-13 11:07  ♂风车车  阅读(4144)  评论(1编辑  收藏  举报