2012-11-05 24 views
0

我想要使用WCF服務。我添加了一個服務引用,現在我試圖把它叫做:爲什麼app.config中缺少<system.serviceModel>部分?

BusStopServiceBinding.BusStopPortTypeClient client = new BusStopServiceBinding.BusStopPortTypeClient(); 

不過,我得到這個錯誤:

Could not find default endpoint element that references contract 'BusStopServiceBinding.BusStopPortType' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

app.config文件看起來像這樣:

<?xml version="1.0"?> 
<configuration> 
<configSections> 
</configSections> 
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup> 
</configuration> 
你可以看到

沒有serviceModel部分。我應該手動添加它嗎?如果是這樣,我應該放入什麼?

+0

一個很好的資源。請通過它(http://msdn.microsoft.com/en-us/library/ms731354.aspx) –

回答

1

本節適用於WCF配置。在Visual Studio的工具部分,你有一個「WCF服務配置編輯器」,它可以幫助你創建這個部分。如果您沒有此部分,則必須使用代碼進行配置,但這不是最佳做法。 在本節中,您必須將終點,安全設置,綁定,wcf合約...

+0

謝謝!在我看到你的答案之前,我已經設法手動添加這部分,它似乎解決了這個問題: '' –

1

如果將服務引用添加到庫而不是您的主項目(exe或web應用程序等),那麼必要的添加將會(通過Visual Studio工具)在庫項目內而不是在您的主項目中生成app.config

然而,在運行時,只有主項目的app.config用,所以你需要提供相關部分從(沒用)app.config庫複製到主項目中的一個。

+0

是的,這是我在網上找到的答案,但在我的情況下只有一個項目。無論如何,這個問題解決了。 –