来自app.config的BasicHttpBinding在代码中不可访问

时间:2014-11-06 17:40:04

标签: c# web-services app-config

我使用服务引用从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.

1 个答案:

答案 0 :(得分: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/");

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