asp.net - 使用 WCF 和 ASP.NET Web 表单传输大文件

标签 asp.net wcf streaming large-files

我正在尝试通过 ASP.NET 网络表单上传大文件(最多 200MB)并将它们存储在数据库中。我能够使用 FileUpload 控件上传大文件,并且我在 SQL Server 中设置了一个字段作为 VarBinary(MAX) FileStream 以在外部存储文件。我想使用 WCF 服务将上传的文件传输到数据库,这就是我遇到的问题。我遇到的唯一问题是当我尝试将文件从后面的代码流式传输到 WCF 服务时。

设置: ASP.NET 应用程序中的客户端 web.config

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IFileTransfer" maxReceivedMessageSize="2147483647"
                 maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
                 transferMode="Streamed" messageEncoding="Mtom"
                 sendTimeout="24.20:31:23.6470000" receiveTimeout="24.20:31:23.6470000">
          <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647"
                        maxDepth="2147483647" maxStringContentLength="2147483647"/>
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:25333/FileTransfer.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IFileTransfer" contract="FileTransfer.IFileTransfer"
        name="BasicHttpBinding_IFileTransfer" />
    </client>
  </system.serviceModel>

WCF 应用程序中的服务器 web.config

<system.serviceModel>
    <services>
      <service name="Ward.POC1.FileStream">
        <endpoint address="ep1" binding="basicHttpBinding" contract="WcfServiceHost.IFileTransfer">
          <identity>
            <dns value="localhost" />
            <certificateReference storeName="My" storeLocation="LocalMachine"
              x509FindType="FindBySubjectDistinguishedName" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" 
                 maxBufferPoolSize="2147483647"
                 transferMode="Streamed" messageEncoding="Mtom" 
                 sendTimeout="24.20:31:23.6470000" receiveTimeout="24.20:31:23.6470000">
          <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
                        maxDepth="2147483647" maxStringContentLength="2147483647"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

我使用了具有以下合约定义的 transferMode="Streamed":

[ServiceContract]
public interface IFileTransfer
{
    [OperationContract]
    Stream GetFile(Guid fileId);

    [OperationContract]
    void AddFile(Stream fileStream);
}

根据配置设置的变化,我会得到以下异常:

The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '24.20:31:23.6100000'.

WCF The remote server returned an error: (400) Bad Request.

任何人都可以为我提供有关使用 WCF 在 ASP.NET 和另一个托管应用程序之间传输上传的大文件的指导吗?我正在尝试流媒体。如果有人有使用二进制数组/分块的优雅解决方案,那么也欢迎。

最佳答案

https://skydrive.live.com/?cid=41ce408c159c69ea&sc=documents&id=41CE408C159C69EA%21321

我有类似的问题。我将 WCF 托管为 WAS 服务并且运行良好。它必须与配置有关,我不确定它是什么?

此外,我注意到的另一件有趣的事情是,如果我将代码保留在由 WCF 本身生成的代码中,它就可以工作。 (Mtom/流配置)。但是如果我将后面的代码(接口(interface)和实现)分离到不同的目录,它就会失败。我最终将相同的代码与 WAS 而不是 IIS 一起使用,并且一切正常。

关于asp.net - 使用 WCF 和 ASP.NET Web 表单传输大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6340110/

相关文章:

c# - 如何通过Azure功能上传大文件?

ffmpeg - LIVE555 RTSP H.264 原始视频文件流 - ffplay 错误

c# - 将逻辑调用上下文从 OWIN 管道传递到 WebApi Controller

c# - 没有使用 Application_Error 记录错误

c# - 电子邮件正文格式问题

c++ - 错误 LNK2019 : unresolved external symbol _soap_ssl_init

c# - 使用机器名和.domain name 创建 URI

iphone - HTTP 自适应比特率流式传输如何在 iPhone 上工作?

.net - 我可以在 VS2008 中使用 .NET Framework 4.0 吗?

c# - 听到 "element ' 行为“具有无效的子元素”应该被忽略,但因此无法更新服务引用