Dot-Net

wcf AddressAlreadyInUseException

  • February 19, 2019

我已經查看了這些文章: wcf System.ServiceModel.AddressAlreadyInUseExceptionWindows 服務通過 HTTPS 託管 WCF升級到 .NET 4.5 後獲取 AddressAlreadyInUseException,它們都沒有解決我的問題。我在 .net 4.5 中有幾個 wcf 服務。它們都在同一個地址中,我在 WCF 服務主機中遇到了這個異常:

System.ServiceModel.AddressAlreadyInUseException: HTTP could not register URL https://+:443/mafawcf01/ServicioAddin.svc/ because TCP port 443 is being used by another application. ---> System.Net.HttpListenerException: The process cannot access the file because it is being used by another process
  at System.Net.HttpListener.AddAllPrefixes()
  at System.Net.HttpListener.Start()
  at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()
  --- End of inner exception stack trace ---
  at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()
  at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)
  at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)
  at System.ServiceModel.Channels.HttpChannelListener`1.OnOpen(TimeSpan timeout)
  at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
  at System.ServiceModel.Channels.DatagramChannelDemuxer`2.OnOuterListenerOpen(ChannelDemuxerFilter filter, IChannelListener listener, TimeSpan timeout)
  at System.ServiceModel.Channels.SingletonChannelListener`3.OnOpen(TimeSpan timeout)
  at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
  at System.ServiceModel.Security.SecurityListenerSettingsLifetimeManager.Open(TimeSpan timeout)
  at System.ServiceModel.Channels.SecurityChannelListener`1.OnOpen(TimeSpan timeout)
  at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
  at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
  at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
  at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
  at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
  at System.ServiceModel.Security.SecuritySessionSecurityTokenAuthenticator.OnOpen(TimeSpan timeout)
  at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
  at System.ServiceModel.Security.SecurityUtils.OpenTokenAuthenticatorIfRequired(SecurityTokenAuthenticator tokenAuthenticator, TimeSpan timeout)
  at System.ServiceModel.Security.SecuritySessionServerSettings.OnOpen(TimeSpan timeout)
  at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
  at System.ServiceModel.Security.SecurityListenerSettingsLifetimeManager.Open(TimeSpan timeout)
  at System.ServiceModel.Channels.SecurityChannelListener`1.OnOpen(TimeSpan timeout)
  at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
  at System.ServiceModel.Channels.DelegatingChannelListener`1.OnOpen(TimeSpan timeout)
  at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
  at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
  at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
  at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
  at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
  at Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info)
System.Net.HttpListenerException (0x80004005): The process cannot access the file because it is being used by another process
  at System.Net.HttpListener.AddAllPrefixes()
  at System.Net.HttpListener.Start()
  at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()

這是我的 app.congif:

   <host>
     <baseAddresses>
       <add baseAddress="https://localhost:443/mafawcf01/ServicioAddin.svc"/>
     </baseAddresses>
   </host>
 </service>
 <service name="mafawcf01.ServicioPing" behaviorConfiguration="mafawcf01.ServicioPingBehavior">
   <endpoint address=""
             binding="basicHttpBinding"
             bindingConfiguration="secureHttpBinding"
             contract="mafawcf01.IServicioPing">
     <identity>
       <dns value="localhost" />
     </identity>
   </endpoint>
   <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
   <host>
     <baseAddresses>
       <add baseAddress="https://localhost:443/mafawcf01/ServicioPing.svc" />
     </baseAddresses>
   </host>
 </service>
 <service behaviorConfiguration="mafawcf01.Service1Behavior" name="mafawcf01.ServicioCargue">
   <endpoint address="https://localhost:443/MafaWCF01/ServicioCargue.svc"
     binding="customBinding" bindingConfiguration="myCustomHttpBinding"
     contract="mafawcf01.IServicioCargue" />
   <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
   <host>
     <baseAddresses>
       <add baseAddress="https://localhost:443/MafaWCF01/ServicioCargue/" />
     </baseAddresses>
   </host>
 </service>
 <service behaviorConfiguration="mafawcf01.Service1Behavior" name="mafawcf01.ServicioValidacionMultiple">
   <endpoint address="https://localhost:443/MafaWCF01/ServicioValidacionMultiple.svc"
     binding="customBinding" bindingConfiguration="transporteSeguro"
     contract="mafawcf01.IServicioValidacionMultiple" />
   <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
   <host>
     <baseAddresses>
       <add baseAddress="https://localhost:443/MafaWCF01/ServicioValidacionMultiple/" />
     </baseAddresses>
   </host>

我已經用這個命令保留了埠:

netsh http add urlacl url=https://+:443/MafaWCF01

還有一個與埠關聯的證書:

netsh http add sslcert ipport=0.0.0.0:443 certhash={some cert hash} appid={some appid}

您需要任何其他資訊嗎?

我發現 WCF 應用程序(或者更確切地說http.sys)在關閉後可以保留 URL 一段時間。如果您嘗試立即重新啟動它,您可能會感到害怕AddressAlreadyInUseException,因為應用程序的前一個實例以某種方式持有它。我發現如果我等幾分鐘再試一次,它就可以工作。

萬一這對任何人都有幫助。嘗試在控制台應用程序中託管 wcf 服務時遇到了同樣的問題。我所做的是打開 wcf 服務庫的屬性(Alt + Enter 或右鍵點擊-> 屬性),然後轉到“屬性”視窗中的 WCF 選項選項卡並取消選中“在同一解決方案中調試另一個項目時啟動 WCF 服務主機”。那麼問題就解決了。

引用自:https://stackoverflow.com/questions/20615405