【问题标题】:I've created a WCF service and now I want to call it from a reference but why InvalidOperationException?我创建了一个 WCF 服务,现在我想从引用中调用它,但为什么是 InvalidOperationException?
【发布时间】:2011-06-08 20:59:28
【问题描述】:

我编写了一个 WCF 服务。我已成功浏览到该服务,它显示:

You have created a service.

然后我使用 Visual Studio 中的“添加服务引用”添加对它的引用。然后我写下面的代码来消费它....

ServiceReference1.VLSContentServiceClient client = new ServiceReference1.VLSContentServiceClient("VLSContentServiceEndpointBehaviour");
List<ServiceReference1.Category> cats = client.GetCategoriesByGET();

但我得到了错误:

找不到端点元素 姓名 'VLSContentServiceEndpointBehaviour' 和合同 'ServiceReference1.IVLSContentService' 在 ServiceModel 客户端 配置部分。这可能是 因为没有配置文件 为您的应用程序找到了,或者因为 没有与此名称匹配的端点元素 可以在客户端元素中找到。

这没有任何意义,因为参数“endPointConfigurationName”与我在服务中设置的匹配。这是服务配置:

  <system.serviceModel>

    <services>
      <service behaviorConfiguration="VLSContentServiceBehaviour" name="VLSContentService">
        <endpoint address="" behaviorConfiguration="VLSContentServiceEndpointBehaviour" binding="webHttpBinding" contract="IVLSContentService"/>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="VLSContentServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>

      <endpointBehaviors>
        <behavior name="VLSContentServiceEndpointBehaviour">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

  </system.serviceModel>

怎么了?

【问题讨论】:

  • 您的端点地址为空?它应该如何找到端点?
  • 检查您的客户端配置..或尝试重新添加服务引用,可能是由于客户端和服务器配置不匹配而添加了一些更改..

标签: c# wcf web-services


【解决方案1】:

您正在使用 REST 服务 - 无法使用添加服务引用创建此类服务的客户端。这仅适用于 SOAP 服务(没有 webHttpBinding 和 webHttp 行为)。此外,一旦您使用 SOAP 服务,您就不会将任何服务器端功能的名称传递给代理的构造函数。代理构造函数需要客户端配置中的客户端端点名称。

How to consume REST service

【讨论】:

    【解决方案2】:

    看来您的合同不正确。你有:

    contract="IVLSContentService"/>
    

    它正在等待:

    contract="ServiceReference1.IVLSContentService"/>
    

    根据错误信息。

    您的端点地址也是空的。这不需要包含一些东西吗?

    【讨论】:

      【解决方案3】:

      创建客户端实例时的 VLSContentServiceEndpointBehaviour 参数是端点的名称,而不是端点行为。

      改变

          <endpoint address="" behaviorConfiguration="VLSContentServiceEndpointBehaviour" binding="webHttpBinding" contract="IVLSContentService"/>
      

          <endpoint address="" name ="Client" behaviorConfiguration="VLSContentServiceEndpointBehaviour" binding="webHttpBinding" contract="IVLSContentService"/>
      

      并创建一个服务客户端

      ServiceReference1.VLSContentServiceClient 客户端 = new ServiceReference1.VLSContentServiceClient("Client");

      你的地址也不见了,这有点奇怪。

      【讨论】:

      • 添加本地服务引用时,端点地址为空,例如在您自己的解决方案中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多