【问题标题】:WCf REST service client side config file emptyWCf REST 服务客户端配置文件为空
【发布时间】:2011-10-13 06:31:54
【问题描述】:

我有一个 REST 服务,并在我的 WPF 应用程序中添加了它的引用。但是,一旦我创建代理的客户端,它就会抛出错误,并且会抛出错误,因为我的客户端 app.config 是空的:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>

客户端的这一行抛出错误:

 HelloWorldClient client = new HelloWorldClient();

这是我在服务器端 web.config 的 system.servicemodel 部分:

 <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <services>
      <service name="WCFRestExample.HelloWorld">
        <endpoint address="" binding="webHttpBinding" contract="WCFRestExample.IHelloWorld"/>        
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </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="true" />

        </behavior>

      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

谁能告诉我为什么 app.config 是空的?我也重新启动了VS2010,但没有运气。

注意:当我直接在浏览器中浏览它时,该服务正在运行。所以,服务器端服务没有问题。

提前致谢:)

【问题讨论】:

  • 创建 ServiceReference 时,请检查 Visual Studio 输出窗口是否有警告和错误。即使您可能在错误列表中看不到任何内容,请检查输出窗口,您是否看到了什么?
  • @Dominik:不!什么都没有。

标签: c# wpf wcf wcf-rest


【解决方案1】:

正如其他一些帖子所提到的(例如After creating a wcf service how do I tell whether its restful or soap from the wsdl?Create a WCF Proxy for a Rest Web Service 等),添加服务参考不适用于 REST 端点。您需要自己创建客户端。

您的 web.config 中的另一个问题:为了创建 REST 端点,您需要同时拥有 webHttpBinding(您可以这样做)&lt;webHttp/&gt; 端点行为添加到您的端点(你没有)。

【讨论】:

  • 感谢您的帮助,但您能说出“自己创建客户端”是什么意思吗?你能给我举个例子吗?或者如果你有它,你可以展示它吗?
  • 我应该在哪里写 。无论我在哪里写,我都会在 web.config 中得到一条红线,这意味着它无法识别该元素。
  • 对于“自己创建客户端”:如果您可以在客户端和服务器之间共享相同的 [ServiceContract] 接口,则可以使用 WebChannelFactory (以及采用单个 Uri 的构造函数参数)来创建一个代理来与服务对话。
  • 元素必须在一个 元素内。
  • REST 入门工具包 2 中提供了 HttpClient 类。我可以使用它吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-19
  • 2011-07-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-08
相关资源
最近更新 更多