加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 21:15:30 所属栏目:百科 来源:网络整理
导读:我有一个WCF服务,它只允许HTTP GET请求: [WebInvoke(Method="GET",ResponseFormat=WebMessageFormat.Json)]public string GetAppData() 使用webHttpBinding公开该服务 system.serviceModelbindings webHttpBinding binding name="AppSvcBinding" security m
我有一个WCF服务,它只允许HTTP GET请求:

[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();

执行此代码时,我收到错误:

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

我检查了fiddler并发现我的客户端正在发送POST消息,而服务期望GET因此出错.

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

解决方法

使用WebGet而不是WebInvoke

编辑

首先将您的方法更改为:

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

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

这修复了它在服务器端.

备份您的客户端代码.

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

然后再次添加服务引用.现在好吧.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读