文件下载需要很长时间

时间:2013-08-06 04:52:14

标签: c# wcf windows-phone-7 windows-phone-8 web-config

我在 WCF 中有网络服务,它从网络服务器下载一个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>

这是我的 ServiceReferences.ClientConfig

<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有什么变化吗?

1 个答案:

答案 0 :(得分:1)

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

您可以等待网络重新启动,或者您可以使用其他网络提供商或第二个网络或许多其他硬件配置来实现此功能,但问题的解决方案是不要让您的网络停止运行第一名。