views:

1607

answers:

2

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>
+2  A: 

You can take a look at WCF Streaming feature.

Dzmitry Huba
+2  A: 

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

marc_s
Thanks for the post. I have already tried this,but it throws the following exception "The remote server returned an error: (400) Bad Request"
Charan
that sounds like something with your config is not okay - can you post client and server config that you have now in your original questions? Just the <system.serviceModel> sections. Thanks!
marc_s
<basicHttpBinding><binding name="HttpStreaming" maxReceivedMessageSize="67108864" transferMode="Buffered"></binding></basicHttpBinding> <service name="WCFFileStreamingDemo.StreamingSample" behaviorConfiguration="WCFFileStreamingDemo.StreamingSampleBehavior"> <endpoint address="" binding="basicHttpBinding" bindingName="HttpStreaming" contract="WCFFileStreamingDemo.IStreamingSample"> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service>
Charan
hi Marc,the above is the config code
Charan
i have added config code in the question
Charan