WCF协议异常:错误的请求400(与http消息大小有关)



我正试图使用WebHttpBinding通过WCF传递一个base64编码的字符串。

我收到服务器没有响应的神秘错误"Bad Request 400"。我知道它与字符串的大小有关,因为如果我测试一个很短的字符串(大约4KB),它就可以工作,但任何稍高的字符串都不行。

我到处都读到这与绑定的web.config中的maxReceivedMessageSize或其他配置有关,但即使在客户端和服务器上更改了这些数字后,我仍然会收到错误(换句话说,我读了很多关于这个确切问题的其他帖子,但它们似乎没有帮助?)

我已经确认,直到以下代码的最后一行出现错误,一切都正常工作:

static IHttpDataPush push = new HttpDataPush();
var wcfClient = ChannelHelperExtensions.WebHttpChannel<IHttpDataRcv>("http://localhost:3941/HttpRcv");
        var args = wcfClient.OptionArgs();
        foreach (var v in args)
        {
            HttpTransactionDataArgs uid = push.Option(v.Entity, v.Option, v.Key);
            wcfClient.ResponseNotification(uid); <-- error thrown at this line

这些是我的服务合同/数据合同:

[ServiceContract]
public interface IHttpDataPush
{
    [OperationContract]
    HttpTransactionDataArgs DbRequest(HttpTransactionOptionArgs args);
    [OperationContract]
    [WebGet]
    HttpTransactionDataArgs DbOption(string entity, string option, string key);
}
[DataContract]
[KnownType(typeof(HttpTransactionDataArgs))]
public class HttpTransactionDataArgs
{
    [DataMember]
    public string EntityName { get; set; }
    [DataMember]
    public string Base64Schema { get; set; }
    [DataMember]
    public string Base64Data { get; set; }
    [DataMember]
    public bool TransactionSuccessful { get; set; }
}

数据推送接收端的合同:

[ServiceContract]
public interface IHttpDataRcv
{
    [OperationContract]
    HttpTransactionOptionArgs[] OptionArgs();
    [OperationContract]
    bool ResponseNotification(HttpTransactionDataArgs args);
}
[DataContract]
[KnownType(typeof(HttpTransactionOptionArgs))]
public class HttpTransactionOptionArgs
{
    [DataMember]
    public string Entity { get; set; }
    [DataMember]
    public string Option { get; set; }
    [DataMember]
    public string Key { get; set; }
}

服务器端web.config:

<bindings>
  <webHttpBinding>
    <binding name="webHttpConfig" closeTimeout="00:20:00" openTimeout="00:20:00"
        receiveTimeout="00:20:00" sendTimeout="00:20:00"
        maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" >
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
          maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    </binding>
<endpoint address="/HttpRcv" behaviorConfiguration="REST" bindingConfiguration="webHttpConfig" binding="webHttpBinding" contract="EmailContracts.IHttpDataRvc" />

客户端web配置(抛出错误的位置:)

<client>
  <endpoint address="http://localhost:3941/HttpRcv"
            binding="webHttpBinding"
            bindingConfiguration="webHttpConfig"
            contract="EmailContracts.IHttpDataRcv" />
</client>
    <bindings>
  <webHttpBinding>
    <binding name="webHttpConfig" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
      <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" 
                    maxDepth="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    </binding>      
  </webHttpBinding>
</bindings>

您是否在IIS/ASP.NET中托管?如果是这样,您还必须增加它们的设置。

对于IIS7,您希望更改system.webServer/security/requestFiltering/requestLimits/@maxAllowedContentLength

对于ASP.NET,您希望更改system.web/httpRuntime/@maxRequestLength

最新更新