C# 文件下载需要很长时间

C# 文件下载需要很长时间,c#,wcf,windows-phone-7,windows-phone-8,web-config,C#,Wcf,Windows Phone 7,Windows Phone 8,Web Config,我在WCF中有一个web服务,它从web服务器下载一个zip文件。我正在使用FtpWebRequest下载该文件。这是我的代码 public byte[] DownloadFile(string fileName) { int bytesRead = 0; String downloadUrl = String.Format("{0}{1}/{2}", "ftp://xx.xx.xxx.xxx/datatransfer/", "folder", fil

我在WCF中有一个web服务,它从web服务器下载一个zip文件。我正在使用FtpWebRequest下载该文件。这是我的代码

public byte[] DownloadFile(string fileName)
    {
        int bytesRead = 0;


        String downloadUrl = String.Format("{0}{1}/{2}", "ftp://xx.xx.xxx.xxx/datatransfer/", "folder", fileName);
        FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(downloadUrl);
        req.Method = WebRequestMethods.Ftp.DownloadFile;
        req.Credentials = new NetworkCredential("uname", "pass");
        Stream reader = req.GetResponse().GetResponseStream();
        //FileStream fileStream = new FileStream(localDestinationFilePath, FileMode.Create);
        byte[] buffer = new byte[2048];
        while (true)
        {
            bytesRead = reader.Read(buffer, 0, buffer.Length);

            if (bytesRead == 0)
                break;

            //fileStream.Write(buffer, 0, bytesRead);
        }
        return buffer;
    }
当我的网络关闭时,下载需要很长时间,并且会出现错误

远程服务器返回错误:NotFound。 System.ServiceModel.ni.dll中出现“System.ServiceModel.CommunicationException”类型的异常,但未在用户代码中处理

这是我的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" maxRequestLength="409600"/>
 </system.web>
 <system.serviceModel>
   <bindings>
     <basicHttpBinding>
      <!--<binding maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" receiveTimeout="00:10:00" closeTimeout="00:10:00">
      <security mode="None" />
    </binding>-->
    <binding closeTimeout="01:10:00"
      openTimeout="01:10:00" receiveTimeout="01:10:00" sendTimeout="01:10:00"
      maxBufferSize="2147483646" maxBufferPoolSize="2147483646" maxReceivedMessageSize="2147483646"
       transferMode="StreamedRequest">
      <readerQuotas maxDepth="2147483646" maxStringContentLength="2147483646" maxArrayLength="2147483646"
        maxBytesPerRead="2147483646" maxNameTableCharCount="2147483646" />
      <security mode="None">             
      </security>
    </binding>    
  </basicHttpBinding>    
</bindings>   

<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
  <protocolMapping>
    <add binding="basicHttpsBinding"  scheme="https"/>
  </protocolMapping>    
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
 </system.serviceModel>
 <system.webServer>
   <modules runAllManagedModulesForAllRequests="true"/>
   <!--
    To browse web app root directory during debugging, set the value below to true.
    Set to false before deployment to avoid disclosing web app folder information.
  -->
   <directoryBrowse enabled="true"/>
 </system.webServer>

</configuration>
<configuration>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647" closeTimeout="01:10:00"
                      openTimeout="01:10:00" receiveTimeout="01:10:00" sendTimeout="01:10:00" maxReceivedMessageSize="2147483647">
                <security mode="None" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localIP/WebService/Service1.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
            contract="MyService.IService1" name="BasicHttpBinding_IService1" />
    </client>
</system.serviceModel>

这是我的servicerences.ClientConfig

<?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" maxRequestLength="409600"/>
 </system.web>
 <system.serviceModel>
   <bindings>
     <basicHttpBinding>
      <!--<binding maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" receiveTimeout="00:10:00" closeTimeout="00:10:00">
      <security mode="None" />
    </binding>-->
    <binding closeTimeout="01:10:00"
      openTimeout="01:10:00" receiveTimeout="01:10:00" sendTimeout="01:10:00"
      maxBufferSize="2147483646" maxBufferPoolSize="2147483646" maxReceivedMessageSize="2147483646"
       transferMode="StreamedRequest">
      <readerQuotas maxDepth="2147483646" maxStringContentLength="2147483646" maxArrayLength="2147483646"
        maxBytesPerRead="2147483646" maxNameTableCharCount="2147483646" />
      <security mode="None">             
      </security>
    </binding>    
  </basicHttpBinding>    
</bindings>   

<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
  <protocolMapping>
    <add binding="basicHttpsBinding"  scheme="https"/>
  </protocolMapping>    
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
 </system.serviceModel>
 <system.webServer>
   <modules runAllManagedModulesForAllRequests="true"/>
   <!--
    To browse web app root directory during debugging, set the value below to true.
    Set to false before deployment to avoid disclosing web app folder information.
  -->
   <directoryBrowse enabled="true"/>
 </system.webServer>

</configuration>
<configuration>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647" closeTimeout="01:10:00"
                      openTimeout="01:10:00" receiveTimeout="01:10:00" sendTimeout="01:10:00" maxReceivedMessageSize="2147483647">
                <security mode="None" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localIP/WebService/Service1.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
            contract="MyService.IService1" name="BasicHttpBinding_IService1" />
    </client>
</system.serviceModel>


当网络关闭时,是否有办法下载大文件。web.config中是否有任何更改

不,当您的网络关闭时,它将关闭。电线上没有电,没有数据。硬件故障没有软件解决方案


您可以等待网络恢复,也可以让另一个网络提供商或第二个网络或许多其他硬件配置来实现这一点,但问题的解决方案是首先不要让网络瘫痪。

网络瘫痪意味着我的互联网速度(下载速度)很慢。那么您可能应该按照错误消息中的说明处理异常。如果网络速度较慢,则可能已超时。更有可能的是,如果您的网络速度异常缓慢,它也可能会出现短暂的中断,而您的服务和FTP所基于的TCP也可能需要持续的连接。