【问题标题】:WCF service upload large file to ftp serverWCF服务上传大文件到ftp服务器
【发布时间】:2013-09-25 06:01:46
【问题描述】:

我正在开发一个 Windows 手机应用程序,它将图像 zip 文件上传到 ftp 服务器。但是我不能上传。它给出了一个错误 Remote server Not found

这是我的 WCF 应用程序 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:30:00" 
      openTimeout="01:30:00" receiveTimeout="01:30:00" sendTimeout="01:30:00" 
      maxBufferSize="2147483646" maxBufferPoolSize="2147483646" maxReceivedMessageSize="2147483646">
      <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>

这是我的 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://xxx.xx.x.xxx/WebService/Service1.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
            contract="MyService.IService1" name="BasicHttpBinding_IService1" />
    </client>
</system.serviceModel>

我创建了两个项目,一个是 windows phone 应用程序,第二个是 wcf 应用程序。我正在向 wcf 服务器发送大型 byte[] 数组,这会给出错误 Remote server Notfound。当byte[] size 很小时它工作得很好,但当尺寸很大时它会失败。我听说我们可以将非常大的文件发送到 wcf 服务,大约 4gb 左右。那我错在哪里?我必须在 web.config 中进行任何更改吗?我有 hosted 我的 wcf 服务到本地计算机上的 IIS

【问题讨论】:

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


    【解决方案1】:

    要通过 wcf 发送大数据,您有两种选择:

    1. 您可以手动将数据拆分为多个片段(例如,从文件中读取 2Kb)并分别传输每个片段。在服务器端,您可以将每个部分保存在临时文件中。这种方法需要一些编码(例如部分的控制顺序)。

    2. 另一种选择是使用 transferMode="Streamed",但此模式有一些限制。

    更新

    如果你因为某些原因不能使用 Streamed 模式,你可以在你的服务中创建一些方法:

    string BeginSendFile();
    

    此方法必须生成id(例如Guid)并将其返回给客户端。服务也可以在临时存储中创建一个具有此名称的文件。

    void SendFilePortion(string id, byte[] data);
    

    您调用此方法并传输一些数据。服务器可能会通过 id 找到临时文件并向其写入数据。

    void EndSentFile(string id, string originalName);
    

    在传输所有数据时调用此方法以重命名您的临时文件并将其替换到非临时存储中。

    【讨论】:

    • 我已经创建了一个包含所有图像的 zip 文件,然后我计算了该 zip 文件的 byte[] 并将 byte[] 发送到 wcf 服务。我试过transferMode="Streamed" 但不行
    • 我使用了 transferMode Streamed。根据您的建议,我已经更改了我的代码。
    • 好的,很高兴为您提供帮助。
    • 也许this link 将有助于流式传输。