2013-07-11 53 views
12

我知道這已被打死,但我無法像它應該那樣工作。 我有幾個合同的WCF服務。 當他們直接打電話時,他們都可以正常工作。 http://merlin.com/CompanyServices/CompanyWcfService.svc/Get_Document_Dates_Received/223278 我已經在InfoPath Forms和Nintex Workflows上成功使用了此WCF服務。 現在我創建一個簡單的ASP.Net應用程序,例如在http://www.webcodeexpert.com/2013/04/how-to-create-and-consume-wcf-services.html中完成。 我能夠添加文章中所述的服務參考。 我添加了一個按鈕的形式,並在Button1_Click事件添加以下代碼:無法找到引用合同的默認端點元素

protected void Button1_Click(object sender, EventArgs e) 
{ 
    ServiceReference1.CompanyWcfServiceClient x = new ServiceReference1.CompanyWcfServiceClient(); 
    var result = x.Get_Document_Dates_Received("223278"); 
} 

當我按一下按鈕我得到的錯誤:

"Could not find default endpoint element that references contract 'ServiceReference1.ICompanyWcfService' 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."

所以我嘗試添加以下內容在web.config:(直接從CompanyWcfService的web.config文件複製

<system.serviceModel> 
<services> 
    <service name="CompanyWcfServices.CompanyWcfService" behaviorConfiguration="ServiceBehavior"> 
    <endpoint address="" binding="webHttpBinding" contract="CompanyWcfServices.ICompanyWcfService" behaviorConfiguration="webHttpEndpointBehavior" > 
     <identity> 
     <dns value="localhost"/> 
     </identity> 
    </endpoint> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">                 
    </endpoint> 
    </service> 
</services> 
<bindings> 
    <webHttpBinding> 
    <binding> 
     <security mode="None"> 
     </security> 
    </binding> 
    </webHttpBinding> 
</bindings> 
<behaviors> 
    <endpointBehaviors> 
    <behavior name ="webHttpEndpointBehavior"> 
     <webHttp helpEnabled ="true" faultExceptionEnabled="true" automaticFormatSelectionEnabled="true"/> 
    </behavior> 
    </endpointBehaviors> 
    <serviceBehaviors> 
    <behavior name="ServiceBehavior"> 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="true" /> 
    </behavior> 
    <behavior name=""> 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="true" /> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/> 
    </system.serviceModel> 

我得到同樣的確切的錯誤,必須有別的東西布萊恩克上。

我終於放棄了,並呼籲像這樣的服務:

HttpWebRequest request = WebRequest.Create(@"http://merlin/Companyservices/CompanyWcfService.svc/Get_Document_Dates_Received/223278") as HttpWebRequest; 
request.Credentials = CredentialCache.DefaultCredentials; 
HttpWebResponse response = null; 

var result = ""; 
try 
{ 
    response = request.GetResponse() as HttpWebResponse; 
    if (response.StatusCode == HttpStatusCode.OK) 
    { 
     using (Stream stream = response.GetResponseStream()) 
     { 
      StreamReader reader = new StreamReader(stream, Encoding.UTF8); 
      result = reader.ReadToEnd(); 
     } 
    } 
} 
catch (Exception ex) 
{ 
    result = ""; 
} 

我花了幾個小時閱讀職位,其中大部分建議的配置信息複製到web.config文件。這對我來說似乎有問題(除了它似乎沒有工作的事實)。如果我需要使用第三方WCF服務怎麼辦?我需要從第三方請求配置信息嗎?而Visa Versa,如果我創建了一個旨在供第三方使用的WCF服務,我是否也需要爲它們提供配置文件?

回答

15

該錯誤表示您沒有在客戶端配置部分中定義的端點。將服務引用添加到項目時,應爲您創建客戶端部分。如果沒有,那麼在system.serviceModel部分中的web.config中爲您的應用程序添加以下

<client> 
    <endpoint 
     name="CompanyWcfService_webhttpBinding" 
     address="http://merlin.com/CompanyServices/CompanyWcfService.svc" 
     binding="webHttpBinding" 
     contract="CompanyWcfServices.ICompanyWcfService" 
     behaviorConfiguration="webHttpEndpointBehavior" 
    /> 
</client> 
+0

另外。在應用程序web.config中的system.ServiceModel節中,您不應該需要services部分。我會刪除它。您只需要在客戶端部分中引用定義的項目。與自定義的行爲配置或綁定設置類似。 –

+0

馬特,謝謝你的迴應。對不起,但我仍然得到同樣的錯誤。 – user1337493

+0

然後這個問題似乎是應用程序中的服務和web.config之間的不匹配。該錯誤表示無法找到具有合同「ServiceReference1.ICompanyWcfService」的服務。請注意,在web應用程序的web.config中,合同是「CompanyWcfServices.ICompanyWcfService」。你有沒有偶然創建一個服務,在你的web應用中添加一個引用,然後更改服務名稱空間? –

9

如果我們有分層的體系結構確保 1)在「所有項目」 2)添加的app.config在所有app.config中添加服務配置細節 3)運行項目

+0

謝謝你提醒我! :) – user1477388

1

實際上這個技巧的訣竅是使用svcutil.exe來創建代理。我一直試圖通過Visual Studio「添加服務」嚮導創建代理。一旦我做到了,配置就變得輕而易舉。

SvcUtil.exe

7

如果你的項目引用庫,並試圖從該庫的函數使用WCF功能,那麼你可以嘗試複製從項目配置文件,以DLL的配置文件中的客戶端的端點。之前發生的一些事情發生在我身上,因爲我在項目中引用的庫不會使用項目配置文件(其中客戶端端點被配置,因爲該服務正在被引用),但它自己的結果是系統找不到端點配置。

+0

同樣在這裏 - 在我的情況下,它是一個引用我的主項目中的共享函數的測試項目。被引用的app.config是測試項目中的app.config,而不是主項目。謝謝! – Jeff

2

將綁定和客戶端值從app.config添加到默認web.config解決了我的問題。

4

在我的情況下,我有一個引用外部UserControl的WPF項目,它具有服務引用。我還必須將該服務參考添加到主項目中。

+0

謝謝!這是我的問題 – Chance

相關問題