2011-04-25 93 views
5

我一直在尝试搜索这个错误,但没有运气到目前为止。上传大的XML到WCF REST服务 - > 400坏请求

所以,我有我的客户机上的服务,这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> 

和我上传这样的文件: -

 using (Stream stream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read)) 
     { 
      try 
      { 
       using (UploadServiceClient upc = new UploadServiceClient()) 
       { 
        upc.UploadFile(stream); 
       } 

      } 
      catch (Exception exc) 
      { 

      } 
     } 

对于小文件它可以正常工作,但对于大型XML文件,这会因400错误请求而失败。我可以做些什么来改变这些设置以获取大型XML文件传输?

感谢您的帮助和时间

更新的客户端的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> 

回答

2

你应该看到如果服务具有相同的maxReceivedMessageSize =“4194304”作为客户端,如果XML确实小于设置的4,194,304字节限制。 WCF默认为64K的maxReceivedMessageSize。

UPDATE:

我注意到你的配置显示配置用于basicHttpBinding的客户端,但配置只能显示的wsHttpBinding。 wsHttpBinding配置将被WCF忽略,因为它不属于basicHttpBinding。如果客户端配置文件没有basicHttpBinding元素,则在.NET 4中使用默认的一个。如果这是真的,那么你会遇到上述64K的限制。

+0

是的都是4194K和消息是小于4G – Johann 2011-04-25 16:28:14

+0

我改变了客户端的app.config文件,并添加了一个basicHTTPBinding,但我仍然得到相同的错误 – Johann 2011-04-26 17:52:46

+0

请尝试替换basicHttpBinding元素(如果有一个)在服务web.config中使用上面显示的更新中客户端配置文件中使用的一个。无论它是否有效,我们都会知道客户端和服务都使用相同的配置。如果您当前在web.config中没有basicHttpBinding元素,则此更改应该可以解决该问题。 – 2011-04-26 18:30:17