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

4
我跟随一些指南在我的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个回答

3

您的客户端配置缺少<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>

服务器名称、IIS虚拟目录和*.svc文件名称(包括扩展名)决定了address=位置。


我刚刚尝试了这个,它给了我相同的错误,我更新了线程,你能看一下吗? - LuxuryWaffles
我得到了以下错误信息:'System.ServiceModel.Diagnostics.TraceUtility'的类型初始化程序引发了一个异常。很抱歉,我对配置文件一无所知。 - LuxuryWaffles
@BloopieBloops: 那是完全无关的事情...... 尝试将客户端配置减少到最少 - 只保留我在响应中展示的内容。没有必要拥有“<services>”节点,也不需要“<serviceBehavior>”和“<serviceHostingEnvironment>”节点 - 这些只针对服务器端有效。 - marc_s
当我输入端点名称为“Default”时,该端点下方有一条蓝线,上面写着元素“system.serviceModel”具有无效的子元素“endpoint”。 - LuxuryWaffles
@BloopieBloops:糟糕——抱歉——您需要一个“<client>”节点来容纳该端点。我已更新我的回复。 - marc_s
更改为新的响应,app.config 中没有错误,但仍然出现“System.ServiceModel.Diagnostics.TraceUtility”的类型初始化程序引发异常。当代码运行时 D: - LuxuryWaffles

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中使用它,该配置文件不会默认创建。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接