How to send a large file from client to server using WCF?

How to send a 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>
+5
source share
3 answers

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

To send large files as a stream to a service, you need to:

  • create a service method that takes Streamas its input parameter
  • create a binding configuration (both on the server and on the client) that uses transferMode=StreamedRequest
  • create a thread in your client and send it to the service method

So, first you need a way in your service contract:

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

Then you need the binding configuration:

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

, , , . filestream , .

, !

+6
+2

readerQuota ( ) maxRequestLength httpRuntime.

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

All Articles