配置 WCF 客户端以使用 HTTP GET 使用 WCF 服务

发布于 2024-10-17 00:00:46 字数 2048 浏览 7 评论 0原文

我有一个仅允许 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) 不允许方法。

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

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

I have a WCF Service which allows only HTTP GET requests:

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

The service is exposed using webHttpBinding

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

I have my client whose config looks like

<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>

My client code is a simple

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

On executing this code I get the error:

The remote server returned an unexpected response: (405) Method Not Allowed.

I checked with fiddler and found that my client is sending a POST message whereas the service expects a GET hence the error.

I wish to know how to configure the client so that is sends GET request to the service.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风无影 2024-10-24 00:00:46

使用 WebGet 而不是 WebInvoke

编辑

首先将您的方法更改为:

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

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

这在服务器端修复了它。

备份您的客户端代码。

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

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

Use WebGet instead of WebInvoke

Edit

Start by changing your method to this:

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

Make sure that webhttpbinding is specified on the server side.

This fixes it on the server side.

Take a backup of your client code.

On the client side delete the service reference. Make sure that all config is removed.

Then add the service reference again. Now it shoud be OK.

寒江雪… 2024-10-24 00:00:46

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

我手动添加了该属性并解决了问题。

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

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

I had a similar problem where the generated proxy service interface on the client was missing the WebGet attribute on my methods.

I added the attribute manually and it resolved the problem.

So it seems like the best current solution is to extract the service interfaces into a separate assembly and then share this assembly between the server and its clients.

The automatic proxy generator seems to be buggy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文