简体   繁体   中英

BasicHttpBinding from app.config inaccessible in code

I have several web services that I am connecting to from a Visual Studio C# project using service references. Two of the service references were created and work without a problem, and one of them took quite a lot of effort to get imported, and now seems to not be working.

I believe the problem lies in the app.config file since it is getting a "Could not find endpoint element" error when I try to create the client.

Here is the 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>

The PrimusService is the one that is not working correctly, and the full error when I try to initialize the client like WebservicePrimusServicePortClient serviceClient = new WebservicePrimusServicePortClient("WebservicePrimusServicePort"); is

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.

I have also tried to simply initialize a BasicHttpBinding object using the binding name and the endpoint name with no luck (short variable names for readability)

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

It throws no error for binding a, but binding b and c fail with the error:

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

While not a direct solution, I ended up just taking the information from the app.config and creating my own BasicHttpBinding and EndpointAddress objects in code. This is more of a workaround than a fix for the problem, and I still don't know why I couldn't access the information in the app.config directly.

I followed the solution in this answer about how to consume a service without using the app.config file.

I created my BasicHttpBinding like

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

and my endpoint like

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

and could connect to the service and retrieve information without a problem, even providing as little information as I did (basically just the address).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM