WCF ProtocolException:错误请求400(与http消息大小相关)

时间:2011-06-08 23:17:43

标签: c# wcf base64 channel webhttpbinding

我正在尝试使用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>

1 个答案:

答案 0 :(得分:1)

您是在IIS / ASP.NET中托管吗?如果是这样,你也必须增加他们的设置。

对于IIS7,您想要更改the system.webServer/security/requestFiltering/requestLimits/@maxAllowedContentLength

对于ASP.NET,您想要更改the system.web/httpRuntime/@maxRequestLength