使用WCF服务传输大文件-检索错误413

问题描述 投票:0回答:1

我知道这个话题已经被讨论了很多次,但是不幸的是所提供的解决方案中没有一个对我有用。我尝试将大型文件(最大1.5 GB)从客户端控制台应用程序传输到WCF服务。但是我总是收到HTTP错误远程服务器返回意外响应:(413)请求实体太大。在传输文件内容时。

我在Internet上找到的所有有关在Web.config文件中添加maxContentLength和类似配置的信息。但是我认为我是从web.config左右输入了错误的内容。

这是我的客户代码用来传输文件的部分:

var fileContent = File.ReadAllBytes(file);
backupSuccessfull = _backupService.TransferFileAsync(sessionId, file, fileHash,
 fileContent).Result;

如果使用同步调用,会发生相同的错误

在接收服务功能中,我使用:

System.IO.File.WriteAllBytes(targetFilePath, fileContent);

但是当我在Visual Studio中放置一个断点时,我可以看到,该方法从未调用过-因此问题必须在网络服务器上,因为我的服务已实例化但尚未使用。

服务器web.config的内容当前看起来像这样:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  </configSections>
  <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 maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" maxAllowedContentLength="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
          <security mode="None"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpBinding" scheme="http"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="false"/>
  </system.webServer>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="BackupDB" connectionString="Data Source=BACKUP16;Initial Catalog=BackupDB-DEV;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</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="BasicHttpBinding_IBackupService"/>
            </basicHttpBinding>
        </bindings>
        <client>
          <endpoint address="http://backup16/WCF/BackupService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IBackupService" contract="BackupServiceReference.IBackupService" name="BasicHttpBinding_IBackupService"/>
        </client>
    </system.serviceModel>
</configuration>

由于我不是web.config配置方面的专家,所以我只是将配置添加到XML的错误部分中。谁能为我提供一些帮助,例如我的web.config看起来如何,以便我可以传输更大的文件。

提前感谢

关于马库斯

c# wcf iis web-config http-status-code-413
1个回答
0
投票

该服务现在使用ProtocolMapping部分发布端点,建议您命名绑定并将配置应用于以下属性。

    <bindings>
      <basicHttpBinding>
        <binding name="mybinding" ... >
        ...
        </binding>
      </basicHttpBinding>
    </bindings>
<protocolMapping>
      <add binding="basicHttpBinding" scheme="http" bindingConfiguration="mybinding"/>
</protocolMapping>

如果不起作用,我们可以使用以下样式来发布服务。

<system.serviceModel>
    <services>
      <service name="WcfService1.Service1">
        <endpoint address="" binding="basicHttpBinding" contract="WcfService1.IService1" bindingConfiguration="mybinding"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
      </service>
</services>

随时让我知道问题是否仍然存在。

© www.soinside.com 2019 - 2024. All rights reserved.