我对 WCF 服务很陌生,当上传的文件太大而无法上传时,我刚刚遇到了这个已知问题。我的情况很奇怪,因为我将maxAllowedContentLength和maxRequestLength都设置为一个很高的数字,所以上传 300 MB 文件应该没有问题......对于小文件,它可以完美运行,所以对我来说,它似乎只是忘记了我的设置。我尝试了很多想法来解决这个问题,所以也许我在我的配置文件中搞砸了,可能有一些不必要的部分,但我仍然找不到解决方案。
我正在使用 IIS 10 和 .Net 4.5。
我的 WCF 服务配置:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" maxRequestLength="4048576" requestLengthDiskThreshold="4048576" executionTimeout="3600" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="TileServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior name="FileUploadServiceBehavior">
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ITileService" sendTimeout="01:00:00" maxBufferSize="5000000" />
<binding name="HttpBinding_MTOM"
messageEncoding="Text"
transferMode="Streamed"
maxReceivedMessageSize="4294967294"
maxBufferSize="65536"
maxBufferPoolSize="65536" />
<binding name="BasicHttpBinding_ITileServiceUpload"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
allowCookies="false"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536"
maxBufferPoolSize="524288"
maxReceivedMessageSize="4294967294"
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/AEGIS.TileService/TileService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ITileServiceUpload" contract="AEGIS.TileService.ITileService" name="BasicHttpBinding_ITileServiceUpload" />
</client>
<services>
<service behaviorConfiguration="FileUploadServiceBehavior" name="AEGIS.TileService.TileService">
<endpoint address="winceStream" binding="basicHttpBinding" bindingConfiguration="HttpBinding_MTOM" contract="AEGIS.TileService.ITileService" />
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" />
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="4294967295" maxUrl="4294967295" maxQueryString="4294967295" />
</requestFiltering>
</security>
我的 ASP.NET Web 应用程序中的 web.config 文件,其中引用了该服务。
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ITileService"/>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/AEGIS.TileService/TileService.svc/winceStream" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ITileService" contract="TileService.ITileService" name="BasicHttpBinding_ITileService"/>
</client>
我的上传方法是这样实现的:
public void UploadFile(FileUploadMessage request)
{
string basePath = AppDomain.CurrentDomain.BaseDirectory + @"\Images\";
string serverFileName = Path.Combine(basePath, request.Filename);
using (FileStream outfile = new FileStream(serverFileName, FileMode.Create))
{
const int bufferSize = 65536; // 64K
Byte[] buffer = new Byte[bufferSize];
int bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outfile.Write(buffer, 0, bytesRead);
bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);
}
}
}
FileUploadMessage 是这样的:
[MessageContract]
public class FileUploadMessage
{
[MessageHeader(MustUnderstand = true)]
public string Filename;
[MessageBodyMember(Order = 1)]
public Stream FileByteStream;
}
编辑1:
我发现这篇关于这个问题的文章。也许我的问题不在于 WCF 服务或配置,而在于 IIS?
我将默认应用程序池的 requestLimits 也设置为 1000000000。错误仍然出现。
同样在选项 3 中,该文章还介绍了更改 ApplicationHost.config。奇怪的是,我在该文件中没有任何关于 requestLimits 的内容。也许这是主要问题?