问题
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?
回答1:
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,...
回答2:
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.
来源:https://stackoverflow.com/questions/13231086/why-is-the-system-servicemodel-section-missing-from-app-config