将大型 xml 上传到 WCF REST 服务 ->400 错误请求 [英] uploading large xml to WCF REST service -> 400 Bad request
问题描述
我一直在尝试搜索此错误,但到目前为止没有运气.
I have been trying to search for this error but no luck so far.
所以我在我的客户端上使用了这个 web.config
So I have a service on my client with this web.config
<system.serviceModel>
<serviceHostingEnvironment>
<baseAddressPrefixFilters>
<add prefix="http://www.mywebsite.com/"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
<services>
<service behaviorConfiguration="ServiceBehavior" name="UploadService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
contract="IUploadService">
<identity>
<dns value="http://www.mywebsites.com/" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior" maxReceivedMessageSize="4194304">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
在客户端我有这个配置
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IUploadService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:30:00" sendTimeout="00:30:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="4194304"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:30:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="">
<extendedProtectionPolicy policyEnforcement="Never" />
</transport>
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://www.mywebsite.com/UploadService.svc"
binding="basicHttpBinding" bindingConfiguration="" contract="IUploadService"
name="WSHttpBinding_IUploadService">
<identity>
<dns value="http://www.mywebsite.com/" />
</identity>
</endpoint>
</client>
我正在上传这样的文件:-
and I am uploading the files like this :-
using (Stream stream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read))
{
try
{
using (UploadServiceClient upc = new UploadServiceClient())
{
upc.UploadFile(stream);
}
}
catch (Exception exc)
{
}
}
对于小文件它工作正常,但对于大的 XML 文件,这会因 400 Bad Request 而失败.如何更改这些设置以传输大型 XML 文件?
for small files it works fine, but for large XML files, this failes with 400 Bad Request. What I can do to change these settings to get large XML file to transfer?
感谢您的帮助和时间
更新的客户端 app.config
UPDATED Client app.config
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basicHttpBinding_IUploadService" receiveTimeout="00:20:00"
bypassProxyOnLocal="true" maxBufferSize="4194304" maxReceivedMessageSize="4194304"
messageEncoding="Mtom" transferMode="Streamed">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security>
<transport>
<extendedProtectionPolicy policyEnforcement="Never" />
</transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://www.mywebsite.com/UploadService.svc"
binding="basicHttpBinding" bindingConfiguration="" contract="IUploadService"
name="basicHttpBinding_IUploadService">
<identity>
<dns value="http://www.mywebsite.com/" />
</identity>
</endpoint>
</client>
推荐答案
您应该查看服务是否与客户端具有相同的 maxReceivedMessageSize="4194304" 以及 XML 是否确实小于设置的 4,194,304 字节限制.WCF 默认为 64K 的 maxReceivedMessageSize.
You should to see if the service has the same maxReceivedMessageSize="4194304" as the client and if the XML is indeed smaller than the 4,194,304 byte limit that is set. WCF defaults to a maxReceivedMessageSize of 64K.
更新:
我注意到您的配置显示为 basicHttpBinding 配置客户端,但配置仅显示 wsHttpBinding.wsHttpBinding 配置将被 WCF 忽略,因为它不属于 basicHttpBinding.如果客户端配置文件没有 basicHttpBinding 元素,那么在 .NET 4 中将使用默认元素.如果这是真的,那么您将遇到上述 64K 限制.
I noticed your config shows the client being configured for basicHttpBinding but the config only shows a wsHttpBinding. The wsHttpBinding config would be ignored by WCF since it doesn't pertain to basicHttpBinding. If the client config file doesn't have a basicHttpBinding element then in .NET 4 a default one is being used. If this is true then you'll run into the 64K limit described above.
这篇关于将大型 xml 上传到 WCF REST 服务 ->400 错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!