【问题标题】:How to Send Large File From Client To Server Using WCF?如何使用 WCF 将大文件从客户端发送到服务器?
【发布时间】:2011-08-24 18:19:16
【问题描述】:

如何在 C# 中使用 WCF 将大文件从客户端发送到服务器?下面是配置代码。

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="HttpStreaming_IStreamingSample" 
                         maxReceivedMessageSize="67108864"
                          transferMode="Streamed">
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint 
            address="http://localhost:4127/StreamingSample.svc"
            binding="basicHttpBinding" 
            bindingConfiguration="HttpStreaming_IStreamingSample"
            contract="StreamingSample.IStreamingSample" 
            name="HttpStreaming_IStreamingSample" />
    </client>
</system.serviceModel>

【问题讨论】:

  • 好的,这是客户端配置。请同时显示服务器配置和服务合同(您调用的方法是什么样的?)

标签: c# wcf stream large-files


【解决方案1】:

正如 Dzmitry 已经指出的那样,您需要查看流媒体。

为了能够将大文件作为流发送到您的服务,您需要:

  • 创建一个接受 Stream 作为其输入参数的服务方法
  • 创建一个使用transferMode=StreamedRequest的绑定配置(在服务器和客户端上)
  • 在您的客户端中创建一个流并将其发送到服务方法

首先,您需要在服务合同中添加一个方法:

[ServiceContract]
interface IYourFileService
{
   [OperationContract]
   void UploadFile(Stream file)
}

那么你需要一个绑定配置:

<bindings>
  <basicHttpBinding>
    <binding name="FileUploadConfig"
             transferMode="StreamedRequest" />
  </basicHttpBinding>
</bindings>

以及使用该绑定配置的服务上的服务端点:

<services>
  <service name="FileUploadService">
     <endpoint name="UploadEndpoint"
               address="......."
               binding="basicHttpBinding"
               bindingConfiguration="FileUploadConfig"
               contract="IYourFileService" />
  </service>
</services>

然后,在您的客户端中,您需要打开例如一个文件流并将其发送到服务方法而不关闭它。

希望有帮助!

马克

【讨论】:

  • 感谢您的帖子。我已经尝试过了,但它会抛出以下异常“远程服务器返回错误:(400)错误请求”
  • 听起来您的配置有些问题 - 您可以在原始问题中发布您现在拥有的客户端和服务器配置吗?只是 部分。谢谢!
  • 服务>
  • 我假设这会将互操作性抛在一边?
  • @fireworks:是的,据我所知以及您在网上可以找到的所有内容,WCF 中基于 http 的流式传输绝对是可互操作的,例如仅适用于两个 .NET WCF 端点。
【解决方案2】:

你可以看看WCF Streaming功能。

【讨论】:

    【解决方案3】:

    除了增加 readerQuota 设置(如上所述)之外,我还必须增加 httpRuntime 属性中的 maxRequestLength。

    <system.web>
        <httpRuntime maxRequestLength="2097151" />
    </system.web>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      相关资源
      最近更新 更多