WCF文件流400错误请求

时间:2011-09-28 16:11:38

标签: wcf httpwebrequest

所以我已经回顾了大量报道的相同问题,由于某种原因,即使使用这些问题的现有解决方案,我也无法获得最终的工作版本。有人能否对我所忽视的内容有所了解?它几乎必须与64k以上的文件上传大小问题有关,因为一旦我尝试传递超过该大小的文件,我几乎立即就会看到错误。

这是我的WCF服务的web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <customErrors mode="Off" />
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <webServices>
      <protocols>
        <add name="HttpGet" />
        <add name="HttpPost" />
      </protocols>
    </webServices>
    <sessionState timeout="60" />
    <httpRuntime maxRequestLength="2097151" useFullyQualifiedRedirectUrl="true" executionTimeout="14400"/>
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <bindings>
      <basicHttpBinding>
        <binding name="FileManager"  maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" transferMode="Streamed">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
          <security mode="None"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="FileManagerBehavior" name="PrimeWebServices.FileManager">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="FileManager" contract="PrimeWebServices.IFileManager"/>
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="FileManagerBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceThrottling maxConcurrentCalls="500" maxConcurrentSessions="500" maxConcurrentInstances="500"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

这是我的WCF服务代码

namespace PrimeWebServices
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "FileManager" in code, svc and config file together.
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class FileManager : IFileManager
    {
        public FileManager()
        {
            HttpContext httpContext = HttpContext.Current;

            if (httpContext != null)
            {
                httpContext.Response.BufferOutput = false;
            }
        }

        public string UploadStream(Stream stream)
        {
            ...
        }
    }
}

最后,这里是客户端配置设置(它是一个winforms客户端,使用服务引用)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IFileManager" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:1116/FileManager.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IFileManager" contract="PrimeWebServices.IFileManager"
                name="BasicHttpBinding_IFileManager" />
        </client>
    </system.serviceModel>
</configuration>

应用程序功能正常,只要文件低于64kB限制我看到其他报告,这让我相信我只是没有正确连接并且它无法恢复到默认配置设置。

3 个答案:

答案 0 :(得分:1)

如果您正在使用IIS,并且安装了请求过滤模块,那么对于默认为28.6MB的请求,会有maxAllowedContentLength个限制。这也是你需要调整的。

示例配置(将限制设置为150MB):

<configuration>
   <system.webServer>
      <security>
         <requestFiltering>
            <requestLimits maxAllowedContentLength="157286400" />
         </requestFiltering>
      </security>
   </system.webServer>
</configuration>

有关详细信息,请参阅此处:http://www.iis.net/ConfigReference/system.webServer/security/requestFiltering/requestLimits

答案 1 :(得分:1)

如果使用ASP.NET开发服务器,则不支持流式传输模式。您需要将服务部署到IIS或WCF服务应用程序以使用流式传输模式。

答案 2 :(得分:0)

在客户端设置中,确保BasicHttpBinding_IFileManager与服务中的FileManager绑定匹配。