2014-11-06 81 views
1

我有幾個Web服務,我使用服務引用從Visual Studio C#項目連接到。其中兩個服務引用被創建並且沒有問題,其中一個引入了相當多的努力,現在似乎沒有工作。從app.config BasicHttpBinding代碼無法訪問

我相信問題在於app.config文件,因爲它在嘗試創建客戶端時出現「無法找到端點元素」錯誤。

這裏是在app.config

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    </configSections> 
    <system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="RateQuoteSoap"> 
        <security mode="Transport" /> 
       </binding> 
       <binding name="RateQuoteSoap1" /> 
       <binding name="QuoteSoap" /> 
       <binding name="WebservicePrimusSoapBinding" /> 
      </basicHttpBinding> 
      <customBinding> 
       <binding name="QuoteSoap12"> 
        <textMessageEncoding messageVersion="Soap12" /> 
        <httpTransport /> 
       </binding> 
      </customBinding> 
     </bindings> 
     <client> 
      <endpoint 
       address="https://webservices.rrts.com/rating/ratequote.asmx" 
       binding="basicHttpBinding" 
       bindingConfiguration="RateQuoteSoap" 
       contract="RoadRunnerService.RateQuoteSoap" 
       name="RateQuoteSoap" /> 
      <endpoint 
       address="http://services.echo.com/Quote.asmx" 
       binding="basicHttpBinding" 
       bindingConfiguration="QuoteSoap" 
       contract="EchoService.QuoteSoap" 
       name="QuoteSoap" /> 
      <endpoint 
       address="http://services.echo.com/Quote.asmx" 
       binding="customBinding" 
       bindingConfiguration="QuoteSoap12" 
       contract="EchoService.QuoteSoap" 
       name="QuoteSoap12" /> 
      <endpoint 
       address="http://api.shipprimus.com/" 
       binding="basicHttpBinding" 
       bindingConfiguration="WebservicePrimusSoapBinding" 
       contract="PrimusService.WebservicePrimusServicePort" 
       name="WebservicePrimusServicePort" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

的PrimusService是一個不正常工作,完整的錯誤,當我嘗試初始化像WebservicePrimusServicePortClient serviceClient = new WebservicePrimusServicePortClient("WebservicePrimusServicePort");客戶端是

System.InvalidOperationException: Could not find endpoint element with name 'WebservicePrimusServicePort' and contract 'PrimusService.WebservicePrimusServicePort'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 name could be found in the client element. 

我也嘗試使用綁定名稱和端點名稱簡單地初始化BasicHttpBinding對象,但沒有運氣(爲了便於閱讀,使用簡短的變量名稱)

BasicHttpBinding a = new BasicHttpBinding("QuoteSoap"); // Works fine 
BasicHttpBinding b = new BasicHttpBinding("WebservicePrimusSoapBinding"); // Fails 
BasicHttpBinding c = new BasicHttpBinding("WebservicePrimusServicePort"); // Fails 

它不引發錯誤的結合,但結合b和c失敗,出現錯誤:

System.Collections.Generic.KeyNotFoundException: No elements matching the key 'WebservicePrimusSoapBinding' were found in the configuration element collection. 

回答

1

雖然不是直接的解決方案,我結束了從app.config中只是把信息和創建我的在代碼中擁有BasicHttpBinding和EndpointAddress對象。這更像是一個解決問題的方法,而且我仍然不知道爲什麼我無法直接訪問app.config中的信息。

我遵循this answer中有關如何在不使用app.config文件的情況下使用服務的解決方案。

我創建像

BasicHttpBinding binding = new BasicHttpBinding(); 
binding.Name = "PrimusServiceBinding"; // Completely Unnecessary 

和我的終點一樣

EndpointAddress endpoint = new EndpointAddress("http://api.shipprimus.com/"); 

和我basicHttpBinding的可以連接到該服務並沒有問題檢索信息,像我一樣,即使提供的信息少(基本上只是地址)。