简体   繁体   English

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

[英]Not able to create a WCF RestFul Service's client

I am newbie with web services. 我是Web服务的新手。 I am trying to create a WCF Restful web service . 我正在尝试创建WCF Restful Web服务。 I have implemented a simple service that accepts parameter name and when the client requests the service it prints hello with name. 我已经实现了一个简单的服务,该服务接受参数名称,并且当客户端请求该服务时,它将使用名称打印问候。

I have created a web client and added the reference of the web service to the client. 我已经创建了一个Web客户端,并将Web服务的引用添加到该客户端。 As per my research, as soon as I add the service reference it should update client's web.config according to the service's web config which is not happening. 根据我的研究,添加服务引用后,它应根据未发生的服务的Web配置更新客户端的web.config The issue is that .config file is not being created with end points after adding the service reference and even after running svcutil.exe..Either way its not generating the required .config file.. 问题是添加服务引用后甚至运行svcutil.exe后,都不会使用端点创建.config文件。无论哪种方式,它都不会生成所需的.config文件。

Relevant code: 相关代码:

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

service web.config 服务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>

what probably could be wrong? 可能是什么错? Any ideas or suggestions would be really appreciated. 任何想法或建议将不胜感激。

The reason that no client endpoint configuration is generated in the config file is that generating client code from a RESTful WCF service is not supported . 在配置文件中未生成客户端端点配置的原因是, 不支持从RESTful WCF服务生成客户端代码。

There is no metadata standard for non-SOAP HTTP services. 对于非SOAP HTTP服务,没有元数据标准。 As a caller of the service you just have to call it directly. 作为服务的调用者,您只需要直接调用它即可。

You can consume a RESTful endpoint in .net in many ways, including: 您可以通过多种方式使用.net中的RESTful终结点,包括:

  1. Use the WebChannelFactory 使用WebChannelFactory
  2. Use the good old HttpClient 使用好旧的HttpClient
  3. Use WebClient 使用WebClient

I found the answer myself. 我自己找到了答案。 In my service I had not declared an endpoint for met data exchange due to which when I was trying to add the reference of the service to the client the required app.config was not being generated. 在我的服务中,我没有声明用于满足数据交换的端点,因此当我尝试将服务的引用添加到客户端时,并未生成所需的app.config。

Adding the end point for metadata exchange solved the problem. 添加用于元数据交换的端点解决了该问题。

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

I hope it would save some one else's lot of time and head banging. 我希望这可以节省别人的大量时间和精力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM