【问题标题】:Configure WCF Client to consume a WCF Service using HTTP GET配置 WCF 客户端以使用 HTTP GET 使用 WCF 服务
【发布时间】:2011-06-24 23:33:34
【问题描述】:

我有一个只允许 HTTP GET 请求的 WCF 服务:

[WebInvoke(Method="GET", ResponseFormat=WebMessageFormat.Json)]
public string GetAppData()

使用 webHttpBinding 公开服务

<system.serviceModel>
<bindings>
  <webHttpBinding>
    <binding name="AppSvcBinding">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" />
      </security>
    </binding>
  </webHttpBinding>
</bindings>
    <behaviors>

我的客户的配置看起来像

<system.serviceModel>
    <client>
        <endpoint address="http://localhost/AppService/Service.svc" 
            binding="webHttpBinding" 
            bindingConfiguration="webHttpBindingConfig"
            contract="AppSvc.IService" 
            behaviorConfiguration="AppSvcBehavior"
            name="AppSvcClient">
    <identity>
      <dns value="localhost"/>
    </identity>
  </endpoint>
    </client>
<bindings>
  <webHttpBinding>
    <binding name="webHttpBindingConfig">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" />
      </security>
    </binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="AppSvcBehavior">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>
</system.serviceModel>

我的客户端代码很简单

ServiceClient client = new ServiceClient("AppSvcClient");
String result = client.GetAppData();

执行此代码时出现错误:

远程服务器返回意外响应:(405) Method Not Allowed。

我检查了提琴手,发现我的客户端正在发送一个 POST 消息,而服务需要一个 GET 因此错误。

我想知道如何配置客户端,以便向服务发送 GET 请求。

【问题讨论】:

  • 服务器端配置的其余部分是什么样的?

标签: c# wcf wcf-binding wcf-client


【解决方案1】:

使用 WebGet 代替 WebInvoke

编辑

首先将您的方法更改为:

[WebInvoke(Method="GET", ResponseFormat=WebMessageFormat.Json,UriTemplate = "/")]
public string GetAppData() 

确保在服务器端指定了 webhttpbinding。

这会在服务器端修复它。

备份您的客户端代码。

在客户端删除服务引用。确保删除所有配置。

然后再次添加服务引用。现在应该没问题了。

【讨论】:

  • WebGet 用于指定服务的行为方式。
  • 我需要在客户端提到同样的事情
  • 更改后需要更新网页引用,客户端才能更新
  • 对于一个webHttpBinding,更新服务引用是没有用的。
  • 我也尝试了 WSHTTPBinding 但没有任何变化。我想知道 Binding 或 Behavior 中的配置更改需要不要告诉 .NET 使服务调用 GET 请求而不是 POST。
【解决方案2】:

我遇到了类似的问题,客户端上生成的代理服务接口在我的方法中缺少WebGet 属性。

我手动添加了属性,它解决了问题。

因此,目前最好的解决方案似乎是将服务接口提取到一个单独的程序集中,然后在服务器及其客户端之间共享该程序集。

自动代理生成器似乎有问题。

【讨论】:

    猜你喜欢
    • 2010-10-24
    • 2016-11-28
    • 1970-01-01
    • 2012-01-10
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多