Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to consume a WCF service. I added a Service Reference, and now I'm trying to call it:

BusStopServiceBinding.BusStopPortTypeClient client = new BusStopServiceBinding.BusStopPortTypeClient();

However, I'm getting this error:

Could not find default endpoint element that references contract 'BusStopServiceBinding.BusStopPortType' 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.

My app.config file looks like this:

<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
</configuration>

No serviceModel section, as you can see. Should I add it manually, and if so, what should I put in it?

This section is for WCF configuration. In the Tools section in visual studio you have a "WCF Service configuration editor" which help you to create this section. If you don't have this section you must configure it in code, but it is not the best practise. In this section you have to put the end point, the security settings, bindings, the wcf contract,...

Thank you! Before I saw your answer, I already managed to add this section manually and it seems to solve this particular problem: <endpoint address="http://.../SiriServerApp/BusStopService?wsdl" binding="basicHttpBinding" contract="BusStopServiceBinding.BusStopPortType" name="MyBinding"/> – Ilya Kogan Nov 5, 2012 at 11:40

If you add the service reference to a library, rather than to your main project (exe, or web application, etc), then the necessary additions will be made (by the Visual Studio tooling) to an app.config inside the library project, rather than in your main project.

However, at runtime, only the app.config of the main project is used, so you would need to copy the relevant parts from the (useless) app.config in the library into the main project's one.

Yes, this is the answer I found all over the web, but in my case there's only one project. Anyway, the issue is solved. – Ilya Kogan Nov 6, 2012 at 12:22

I had the same error, and Damien_The_Unbeliever' s answer helped me understand && solve my problem: "However, at runtime, only the app.config of the main project is used".
I was having my wsdl proxy in a class library project. Although my UnitTest project was able to read from that class library's app.config, but at runtime that class lib's app.config became irrelevant.

My solution was to create a partial class for the auto-generated client class and rely on the System.ServiceModel configured from code instead of reading it from who knows what app.config:

public partial class WsdlClient 
        public WsdlClient (string endpointUrl, TimeSpan timeout, string username, string password) :
            base(WsdlClient.GetBindingForEndpoint(timeout), WsdlClient.GetEndpointAddress(endpointUrl))
            this.ChannelFactory.Credentials.UserName.UserName = username;
            this.ChannelFactory.Credentials.UserName.Password = password;           
        private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(TimeSpan timeout)
            var httpsBinding = new BasicHttpsBinding();
            httpsBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
            httpsBinding.Security.Mode = BasicHttpsSecurityMode.Transport;
            var integerMaxValue = int.MaxValue;
            httpsBinding.MaxBufferSize = integerMaxValue;
            httpsBinding.MaxReceivedMessageSize = integerMaxValue;
            httpsBinding.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
            httpsBinding.AllowCookies = true;
            httpsBinding.ReceiveTimeout = timeout;
            httpsBinding.SendTimeout = timeout;
            httpsBinding.OpenTimeout = timeout;
            httpsBinding.CloseTimeout = timeout;
            return httpsBinding;
        private static System.ServiceModel.EndpointAddress GetEndpointAddress(string endpointUrl)
            if (!endpointUrl.StartsWith("https://"))
                throw new UriFormatException("The endpoint URL must start with https://.");
            return new System.ServiceModel.EndpointAddress(endpointUrl);
WsdlClient MyWsdlClient =>
            new WsdlClient (ConfigurationManager.AppSettings["endpointUrl"]
                , new TimeSpan(0, 0, 1, 0),
                "blabla",
                "blabla");
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.