[英]WCF ProtocolException : Bad Request 400 (related to http message size)
我正在嘗試使用 WebHttpBinding 在 WCF 上傳遞 base64 編碼字符串。
我收到服務器沒有響應的神秘錯誤“錯誤請求 400”。 我知道它與字符串的大小有關,因為如果我測試一個非常短的字符串(大約 4KB 左右)它可以工作,但任何稍高的字符串都不會。
我到處讀到這與 maxReceivedMessageSize 或 web.config 中的其他配置有關,但即使在客戶端和服務器上更改了這些數字后,我仍然收到錯誤(換句話說,我已經閱讀了很多其他關於這個確切問題的帖子,但他們似乎沒有幫助?)
我已經確認一切正常,直到引發錯誤的以下代碼的最后一行:
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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.