2012-06-15 53 views
0

我想編程設置WCF連接的終點。WCF:編程設置端點

我還沒有能夠,下面是我使用的代碼有人幫助識別出了什麼問題?

 Uri wsBaseAddress = new Uri("http://localhost:27198/"); 

     ServiceHost host = new ServiceHost(typeof(ServiceClient), wsBaseAddress); 

     WSHttpBinding wshttpbinding = new WSHttpBinding(); 

     host.AddServiceEndpoint(typeof(IService), wshttpbinding, "ServiceClient"); 
     host.AddServiceEndpoint(typeof(IService), wshttpbinding, 
      "http://localhost:27198/Service.svc"); 

     host.Open(); 

編輯:

Error: HTTP could not register URL "http://+:27198/" because TCP port 27198 is being used by another application."

謝謝

+0

什麼是不工作?你有錯誤嗎? – cadrell0

+0

@ cadrell0對不起,請參閱我的編輯。我真的不明白爲什麼錯誤正在發生。 – user101010101

回答

1

HTTP could not register URL "http://+:27198/" because TCP port 27198 is being used by another application."

這幾乎說明了一切。還有另一個應用程序當前正在該端口上進行偵聽,並且由於只有一個應用程序可以在給定時刻綁定到給定的端口和IP,因此您的程序不能。

執行netstat -abncmd中查看哪個程序最有可能是ASP.NET開發服務器。

如果沒有其他程序綁定到該端口,則可以嘗試以管理員身份運行Visual Studio。

+0

但這是在我的客戶端app.config中的地址相同,它以前如何工作? – user101010101

+0

@ user101010101檢查您的客戶端應用程序當前是否正在運行並偵聽該端口,然後嘗試啓動第二個實例。 –

0

下面你不提供服務名稱雖然沒有在最後聲明中使用添加端點

Uri wsBaseAddress = new Uri("http://localhost:27198/"); 

應該

Uri wsBaseAddress = new Uri("http://localhost:27198/Service"); 

你必須提供該服務的名稱,而不是聲明帶擴展名的服務文件名稱

更改

host.AddServiceEndpoint(typeof(IService), wshttpbinding, 
      "http://localhost:27198/Service.svc"); 

host.AddServiceEndpoint(typeof(IService), wshttpbinding, 
      "http://localhost:27198/Service"); 
+0

我厭倦了這個建議,但同樣的錯誤發生了! – user101010101

+0

你得到了什麼錯誤,你服務正在運行?您託管服務的位置? – Adil

+0

{「HTTP無法註冊URL http:// +:27198/Service /,因爲TCP端口27198正在被另一個應用程序使用。」}。與以前相同的錯誤。它在我的本地機器上託管,是的,我檢查服務正在運行。 – user101010101