我知道这个话题已经被讨论过多次,但不幸的是,所提供的解决方案都不适合我。我尝试将大文件(最大 1.5 GB)从客户端控制台应用程序传输到 WCF 服务。但我总是收到 HTTP 错误远程服务器返回了意外响应:(413)请求实体太大。在传输文件内容时。
我在互联网上找到的所有关于将 maxContentLength 和类似配置添加到 web.config 文件的信息。但我假设我在 web.config 左右的错误部分输入了它们。
编辑 26.02.2020 18:35(由于提示和新测试而更新)
根据上面的提示,我在配置文件中添加了一些条目并进行了更多测试。与此同时,我发现了一些事情:
- web.config 中的数字定义了我在几页上阅读的位而不是字节的大小
- 该数字必须是有效的int32 - 因此最大值为2147483647
- 2147483647 位大约是256 MB - 所以可以理解,我的测试文件大约 400 MB 引起了问题
总体而言,如果无法传输大文件 - 至少20-30 MB应该是可能的。对于较大的文件,我会找到其他解决方案。
为了进行更简单的测试,我刚刚创建了一个新的空 WCF 服务和一个控制台应用程序来测试它。
您可以在Google Drive上找到完整的源代码。我还包含了一个 22MB 的测试图像,它无法传输。
与我的第一个问题不同,我现在得到的是 404 错误而不是 413。因此,当请求与服务不匹配时,IIS 会返回 404 而不是之前的 413。对我来说这是一个非常奇怪的行为。
web.config 和 app.config 看起来仍然和以前一样(除了没有实体框架的东西)。
服务器 web.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="mybinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<security mode="None"/>
</binding>
</basicHttpBinding>
</bindings>
<protocolMapping>
<add binding="basicHttpBinding" scheme="http" bindingConfiguration="mybinding" />
</protocolMapping>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
客户端 app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="mybinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:53042/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="mybinding" contract="ServiceReference1.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
由于我不是 web.config 配置方面的专家,我假设我只是在 XML 的错误部分添加了配置。任何人都可以为我提供一些帮助,我的 web.config 需要看起来如何,我可以传输更大的文件。
提前致谢
问候马库斯