我已经创建了一个WCF服务,现在我想从引用中调用它,但为什么会出现InvalidOperationException?

Exi*_*tos 1 c# wcf web-services

我写了一个WCF服务.我已成功浏览该服务,它说:

You have created a service.
Run Code Online (Sandbox Code Playgroud)

然后,我使用visual studio中的"添加服务引用"添加对它的引用.然后我编写以下代码来使用它....

ServiceReference1.VLSContentServiceClient client = new ServiceReference1.VLSContentServiceClient("VLSContentServiceEndpointBehaviour");
List<ServiceReference1.Category> cats = client.GetCategoriesByGET();
Run Code Online (Sandbox Code Playgroud)

但我得到错误:

找不到名为"VLSContentServiceEndpointBehaviour"的端点元素,并在ServiceModel客户端配置部分中收缩"ServiceReference1.IVLSContentService".这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此名称匹配的端点元素.

这没有任何意义,因为参数'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>
Run Code Online (Sandbox Code Playgroud)

这是怎么回事?

Lad*_*nka 5

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

如何使用REST服务