微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有一个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.

这修复了它在服务器端.

备份您的客户端代码.

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐