【问题标题】:Problem with downloading many files from WCF从 WCF 下载许多文件的问题
【发布时间】:2010-10-14 08:52:09
【问题描述】:

我遇到了 WCF 服务问题。当我下载 2 个文件时,一切正常(不到 1 分钟),但是当我尝试下载 3 个以上的文件时,就会出现问题。我一直在等待,什么都没有:/ 每个文件大约有 1 MB。

Dictionary<FileIdentifier, Stream> data = new Dictionary<FileIdentifier, Stream>();

 foreach (string path in paths)
 {

    using (var client = new ServiceClient())
    {
        var stream = client.GetFile(path);

        data[fileIdentifier] = stream;
     }

 }

WCF 服务中的方法:

public Stream GetFile(string path)
        {
             FileStream fs = new FileStream(stream, FileMode.Open);
            fs.Close();
            return fs;

        }

WCF 服务配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <system.serviceModel>
    <client />
    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBindingConfiguration" closeTimeout="00:10:00"
          openTimeout="00:15:00" receiveTimeout="00:15:00" sendTimeout="00:15:00"
          allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
          maxBufferSize="2097152000" maxBufferPoolSize="524288000" maxReceivedMessageSize="2097152000"
          messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed"
          useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="524288000"
            maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="FileServer.Service" behaviorConfiguration="FileServer.Service1Behavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8731/Design_Time_Addresses/Server.FileServer/Service/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address="" contract="FileServer.IService" binding="basicHttpBinding" bindingConfiguration="basicHttpBindingConfiguration">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="FileServer.Service1Behavior">
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.web>
    <compilation debug="true" />
    <httpRuntime maxRequestLength="2097151" executionTimeout="1000" />
    <customErrors mode="RemoteOnly" />
  </system.web>
  <system.webServer>
    <directoryBrowse enabled="false" />
  </system.webServer>
</configuration>

【问题讨论】:

  • IIRC,这是 HTTP 规范的限制。您对此无能为力(据我所知)。为什么要通过 WCF 下载文件?当然,带宽开销将是“巨大的”。
  • 我正在使用这台服务器甚至下载大小为 2gb 的文件,所以我不知道这是否是 HTTP 规范限制的真正问题:/ 但我是新手:/
  • @user278618:对不起,我应该提到限制是并发连接到同一台服务器。这卡在 2。
  • @leppie:可以通过设置ServicePointManager.DefaultConnectionLimit来改变
  • @Ladislav Mrnka:不知道,谢谢。

标签: wcf .net-3.5 streaming


【解决方案1】:

您不应该在服务端关闭流。

你会怎么做:

  1. 在服务上打开流
  2. 向客户端返回流
  3. 在客户端读取流
  4. 在客户端关闭流
  5. WCF 将为您关闭服务流

【讨论】:

  • +1 不知道为什么它似乎适用于 1 或 2 个文件,但是是的,您不应该关闭服务器端的流。
【解决方案2】:

不返回流,而是将流读入字节数组,然后返回字节数组。

【讨论】:

  • 这意味着数据将被缓冲并且消息大小可能会很大(每个文件 1MB)。流媒体是正确的方法(请参阅@Johann 的回答)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多