无法创建 WCF RestFul 服务的客户端

Not able to create a WCF RestFul Service's client

我是网络服务新手。我正在尝试创建一个 WCF Restful 网络服务。我实现了一个接受参数名称的简单服务,当客户端请求该服务时,它会打印 hello with name。

我创建了一个网络客户端并将网络服务的引用添加到客户端。 根据我的研究,只要我添加服务引用,它就会根据服务的 Web 配置 更新客户端的 web.config,但这并没有发生。 问题是在添加服务引用之后甚至在 运行 svcutil.exe 之后都没有创建带有端点的 .config 文件。无论哪种方式,它都没有生成所需的 .config 文件。 .

相关代码:

[ServiceContract]
    public interface IRestFulTest
    {
        [OperationContract]
    [WebInvoke(Method= "GET",ResponseFormat=WebMessageFormat.Xml, UriTemplate="xml/{Name}")]
        String sayhello(String name);
    }

服务web.config

 <system.serviceModel>
    <services>
      <service name="ServiceDemo.RestFulTest">
        <endpoint behaviorConfiguration="WebBehavior" binding="webHttpBinding"
          bindingConfiguration="" contract="ServiceDemo.IRestFulTest" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

可能出了什么问题? 任何想法或建议将不胜感激。

配置文件中未生成客户端端点配置的原因是从 RESTful WCF 服务生成客户端代码 is not supported

没有针对非 SOAP HTTP 服务的元数据标准。作为服务的调用方,您只需直接调用它即可。

您可以通过多种方式在 .net 中使用 RESTful 端点,包括:

  1. 使用WebChannelFactory
  2. 使用旧的HttpClient
  3. 使用WebClient

我自己找到了答案。在我的服务中,我没有为 met 数据交换声明一个端点,因此当我试图将服务的引用添加到客户端时,没有生成所需的 app.config。

为元数据交换添加端点解决了问题。

 **<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>**

我希望它能节省其他人的大量时间和头脑。