简体   繁体   中英

Error when using service: “Could not find default endpoint element that references contract”

I'm trying to switch my WCF service from TCP bindings to HTTP bindings by modifying app.config , but when I try consuming the WCF service from a test console application, I get this error:

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

Here's the newer app.config using HTTP bindings that gives me the error above:

<services>
  <service name="Test.UserServ" behaviorConfiguration="ServiceBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8000/Users" />
      </baseAddresses>
    </host>
    <endpoint 
        address="" 
        binding="webHttpBinding" behaviorConfiguration="EndpointBehavior" 
        contract="Test.IUsers" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="EndpointBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>

Here's the older working app.config that still uses TCP bindings:

<services>
  <service name="Test.UserServ" behaviorConfiguration="TCPBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8731/Users"/>
      </baseAddresses>
    </host>
    <endpoint 
        address="" 
        binding="netTcpBinding" bindingConfiguration="TCPConfiguration" 
        contract="Test.IUsers"/>
    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
  </service>
</services>
<bindings>
  <netTcpBinding>
    <binding name="TCPConfiguration">
      <security mode="None"/>
    </binding>
  </netTcpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="TCPBehavior">
      <serviceMetadata/>
    </behavior>
  </serviceBehaviors>
</behaviors>

Is there something wrong with the configuration or is this due to some limitation imposed by the HTTP bindings I've switched to?

Most likely your client config file specifies two endpoints. Both the old TCP endpoint and your new HTTP endpoint. Either delete the old TCP endpoint in your client config file or use the constructor when declaring the client in your code (the constructor takes the name of an endpoint as a string, smth like new YourClient("NameOfWsHttpBinding"); ).

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