5

如何在 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>
4

3 回答 3

6

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

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

  • 创建一个接受 aStream作为其输入参数的服务方法
  • 创建一个绑定配置(在服务器和客户端上),它使用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>

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

希望有帮助!

马克

于 2009-10-05T11:57:49.317 回答
2

您可以查看WCF Streaming功能。

于 2009-10-05T11:26:03.343 回答
2

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

<system.web>
    <httpRuntime maxRequestLength="2097151" />
</system.web>
于 2011-01-04T19:37:23.360 回答