使用WCF服务 - 找不到默认端点元素

时间:2013-07-27 07:26:16

标签: c# wcf visual-studio-2008

我遵循了一些指南,在我的Windows应用程序中使用WCF服务。 我的WCF服务适用于我的移动应用程序,但我无法在Windows应用程序上运行。

尝试运行代码时生成的错误是:

  
    

无法在ServiceModel客户端配置部分中找到引用合同“AllocationService.IAllocatingService”的默认端点元素。这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素。

  

调用Web服务方法:

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

Web服务方:

    [OperationContract]
    void notifyZoneChanged(int LocationID);

Web Service的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>

Windows应用程序的App.config:

<?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>

2 个答案:

答案 0 :(得分:2)

您的客户端配置缺少定义连接位置的<endpoint>节点 - 因此您需要将其添加到您的配置中:

<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>

address=位置取决于托管服务器的服务器名称,*.svc文件所在的IIS虚拟目录以及*.svc文件本身的名称(包括扩展名)

答案 1 :(得分:0)

您需要放置终点

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

在需要使用它的web.config中,所有使用该方法的“层”!

示例:如果您在BLL(您创建的逻辑方法)中调用它,并在PL(Web部件,HTML)中使用它。在BLL web.config中,默认情况下将创建终点,但是您将在默认情况下不会创建的PL web.config中使用它。