【问题标题】:BasicHttpBinding from app.config inaccessible in code代码中无法访问 app.config 中的 BasicHttpBinding
【发布时间】:2015-01-03 08:06:56
【问题描述】:

我有几个使用服务引用从 Visual Studio C# 项目连接到的 Web 服务。其中两个服务引用已创建并且可以正常工作,其中一个需要花费大量精力才能导入,现在似乎无法正常工作。

我认为问题出在 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

绑定a没有报错,但是绑定b和c失败,报错:

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

【问题讨论】:

    标签: c# web-services app-config


    【解决方案1】:

    虽然不是直接解决方案,但我最终只是从 app.config 中获取信息并在代码中创建自己的 BasicHttpBinding 和 EndpointAddress 对象。这与其说是解决问题,不如说是一种解决方法,我仍然不知道为什么我无法直接访问 app.config 中的信息。

    我遵循this answer 中的解决方案,了解如何在不使用 app.config 文件的情况下使用服务。

    我创建了我的 BasicHttpBinding 像

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

    和我的端点一样

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

    并且可以毫无问题地连接到服务并检索信息,甚至提供与我一样少的信息(基本上只是地址)。

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 2014-01-22
      • 1970-01-01
      相关资源
      最近更新 更多