如何使用WCF从客户端向服务器发送大文件? [英] How to Send Large File From Client To Server Using WCF?

查看:48
本文介绍了如何使用WCF从客户端向服务器发送大文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C#中的WCF从客户端向服务器发送大文件?在配置代码下方.

How to Send Large File From Client To Server Using WCF in C#? Below the configuration code.

<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>

推荐答案

正如Dzmitry所指出的那样,您需要检查流式传输.

You need to check out streaming, as Dzmitry already pointed out.

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

In order to be able to send large files as a stream to your service, you'll need to:

  • 创建一个接受 Stream 作为其输入参数的服务方法
  • 在服务器和客户端上创建一个绑定配置,该配置使用 transferMode = StreamedRequest
  • 在客户端中创建流并将其发送到服务方法
  • create a service method that accepts a Stream as its input parameter
  • create a binding configuration (on both the server and the client) which uses transferMode=StreamedRequest
  • create a stream in your client and send it to the service method

因此,首先,您需要在服务合同中提供一种方法:

So first off, you need a method in your service contract:

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

然后您需要一个绑定配置:

Then you need a binding configuration:

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

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

and a service endpoint on your service using that binding configuration:

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

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

and then, in your client, you need to open e.g. a filestream and send that to the service method without closing it.

希望有帮助!

马克

这篇关于如何使用WCF从客户端向服务器发送大文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆