天天看點

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>

這樣,服務就可以正常了。

    本文轉自風車車  部落格園部落格,原文連結:http://www.cnblogs.com/xray2005/archive/2010/01/13/1646327.html,如需轉載請自行聯系原作者

繼續閱讀