How to Send Large File From Client To Server Using WCF?

梦想的初衷 提交于 2019-12-01 05:44:24

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:

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

Hope that helps!

Marc

You can take a look at WCF Streaming feature.

In addition to increasing readerQuota settings (mentioned above) I had to also up the maxRequestLength inside the httpRuntime attribute.

<system.web>
    <httpRuntime maxRequestLength="2097151" />
</system.web>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!