简体   繁体   中英

Consume WCF Service - Could not find default endpoint element

I followed some guide to consuming WCF service in my windows application. My WCF Service worked well for my mobile application but I cannot get it working on windows application.

The error generated when i try to run the code is:

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

Calling web service method:

    AllocationService.AllocatingServiceClient client = new AllocationService.AllocatingServiceClient();
    client.notifyZoneChanged(1);

Web Service side:

    [OperationContract]
    void notifyZoneChanged(int LocationID);

Web Service's web.config:

  <?xml version="1.0"?>
  <configuration>
    <connectionStrings>
      <add name="PCSDB" connectionString="Data Source=alj6d2eqa0.database.windows.net;Initial Catalog=StaffAllocatorDB;Persist Security Info=True;User ID=---;Password=---" providerName="System.Data.SqlClient" />
    </connectionStrings>
    <system.web>
      <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>
      <services>
        <service name ="StaffAllocator.AllocatingService">
          <endpoint address="" behaviorConfiguration="AllocationBehavior" binding="webHttpBinding" bindingConfiguration="" contract="StaffAllocator.IAllocatingService">
          </endpoint>
        </service>
      </services>
      <behaviors>
        <serviceBehaviors>
          <behavior>
            <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
            <serviceMetadata httpGetEnabled="true"/>
            <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
            <serviceDebug includeExceptionDetailInFaults="false"/>
          </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
          <behavior name="AllocationBehavior">
            <webHttp/>
          </behavior>
        </endpointBehaviors>
      </behaviors>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>

  </configuration>

App.config for windows application:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="PCSDB" connectionString="Data Source=alj6d2eqa0.database.windows.net;Initial Catalog=StaffAllocatorDB;Persist Security Info=True;User ID=---;Password=---" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AllocationBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <endpoint Name="Default"
              address="http://staffallocatingsystem.cloudapp.net/AllocatingService.svc"
              binding="webHttpBinding"
              behaviorConfiguration="AllocationBehavior"
              contract="AllocationService.IAllocatingService" />
  </system.serviceModel>
</configuration>

Your client side config is missing an <endpoint> node that would define where to connect to - so you need to add this to your config :

<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AllocationBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <client>
       <endpoint Name="Default"
                 address="http://yourserver/virtualweb/YourService.svc"
                 binding="webHttpBinding"
                 behaviorConfiguration="AllocationBehavior"
                 contract="AllocationService.IAllocatingService" />
    </client>
</system.serviceModel>

The address= location is determined by the server name where your server is hosted, the IIS virtual directory where your *.svc file lives, and the name of the *.svc file itself (including the extension)

You need to put the end point

<system.serviceModel>
    <client>
       <endpoint Name="Default"
                 address="http://yourserver/virtualweb/YourService.svc"
                 binding="webHttpBinding"
                 behaviorConfiguration="AllocationBehavior"
                 contract="AllocationService.IAllocatingService" />
    </client>
</system.serviceModel>

in the web.config where you need to use it, all the "layers", where the method is used!

Example: if you call it in BLL (logic methods that you craeted) and use it in PL (the web part, HTML). In the BLL web.config the end point will be created by default but you gonna need it in the PL web.config that will not be created by default.

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