I’m building a .NET component that will call an external web service. I used the "Add Service Reference" dialog to add the web service to my component, which generates the code needed to consume the service and adds the settings to the app.config file.
I’m testing the component by adding a reference to its DLL from a Console application and calling the appropriate method that creates a new instance of the web service: ... = new MyServiceSoapClient(). However, when I do this, I get the following exception:
InvalidOperationException
Could not find default endpoint element that references contract ‘MyServicesSoap’ 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 contract could be found in the client element.
This makes sense since the app.config isn’t being brought over with the component’s DLL. How can I call the web service without having to rely on the settings in the App.Config?
The settings in
<system.ServiceModel>in the app.config file will tell the component how to connect to the external web service. The xml is simply a textual representation of the necessary classes and enumerations required to make the default connection to the web service.For example, this is the code that was generated for the web service that I added:
This can be translated to code like so:
Usually, when you use the parameterless constructor (i.e.
new MyServicesSoapClient()), the settings in the app.config file will be used. However, you can bypass the app.config file by explicitly setting thebindingandendpointvalues in code and passing those instances into the constructor.