-1

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

4

1 回答 1

1

不,当您的网络出现故障时,它就会出现故障。电线上没有电,没有数据。硬件故障没有软件解决方案。

您可以等待您的网络恢复正常,或者您可以让另一个网络提供商或第二个网络或许多其他硬件配置来完成这项工作,但解决您的问题的方法是首先不要让您的网络出现故障。

于 2013-08-06T04:55:35.940 回答