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

标签 c# wcf visual-studio-2008

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

当我尝试运行代码时产生的错误是:

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.

调用网络服务方法:

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

网络服务端:

    [OperationContract]
    void notifyZoneChanged(int LocationID);

Web 服务的 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>

最佳答案

您的客户端配置缺少 <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=位置由托管服务器的服务器名称决定,IIS 虚拟目录是您的 *.svc文件存在,*.svc 的名称文件本身(包括扩展名)

关于c# - 使用 WCF 服务 - 找不到默认端点元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17895369/

相关文章:

wcf - 使用 NetTcpBinding 与非 WCF 客户端通信

windows - 图像析构函数中的访问冲突

.NET Visual Studio 生成错误 "another partial declaration of this type exists"

c# - 卸载 Windows 服务的正确方法?

c# - 找不到服务客户端

c# - 如何设置消息队列的所有者?

c# - ASP.NET Core 2.2 在多对多插入时返回 "NotSupportedException: Collection was of a fixed size"

c# - WPF 应用程序的异常处理

c# - 字符串长度昂贵

c# - wcf json 网络服务