2014-11-06 129 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的可以连接到该服务并没有问题检索信息,像我一样,即使提供的信息少(基本上只是地址)。