Reputation: 22389
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?
Upvotes: 0
Views: 4223
Reputation: 1
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");
Upvotes: 0
Reputation: 239814
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.
Upvotes: 2
Reputation: 178
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,...
Upvotes: 2