#TECH

Large File Streaming using WCF

File Streaming using WCF

Often there is a need to send large file (more than 100 mb) from client to service. NetTcpBinding has been used along with WCF for the code sample however HttpBinding would be similar.

The main point in the implementation is to create binding which is NetTcpBinding_FileUpload in this case and set transfermode to “Streamed”.

Below code sample is self-explanatory but in case any step is not clear please let me know.

Service end point configuration

Code Snippet
  1. <service name=”LargeFileService.Service.LargeFileService” behaviorConfiguration=”LargeFileServiceBehavior”>
  2.         <host>
  3.           <!– Create base address–>
  4.           <baseAddresses>
  5.             <add baseAddress=”net.tcp://localhost:19999/LargeFileService.svc”/>
  6.           </baseAddresses>
  7.         </host>
  8.         <!– Create end point, pleaes note binding configuration NetTcpBinding_FileUpload–>
  9.         <endpoint address=”/FileUpload” binding=”netTcpBinding” bindingConfiguration=”NetTcpBinding_FileUpload”
  10.                   contract=”LargeFileService.Services.App.ILargeFileService”/>
  11.         <!– Metadata Endpoints –>
  12.         <!– The Metadata Exchange endpoint is used by the service to describe itself to clients. –>
  13.         <!– This endpoint does not use a secure binding and should be secured or removed before deployment –>
  14.         <endpoint address=”mex” binding=”mexTcpBinding” contract=”IMetadataExchange”/>
  15.       </service>

 

Large file binding configuration

Code Snippet
  1. <bindings>
  2.       <netTcpBinding>
  3.          <binding name=”NetTcpBinding_FileUpload” transferMode=”Streamed”
  4.                  maxReceivedMessageSize=”9223372036854775807″  receiveTimeout=”01:00:00″
  5.                  sendTimeout=”00:10:00″  closeTimeout =”0:01:00″ openTimeout=”0:01:00″>
  6.         </binding>
  7.       </netTcpBinding>
  8.     </bindings>

 

Service Contract

Code Snippet
  1. [OperationContract]
  2. void UploadFile(FileContent item);

 

Service Implementation

Code Snippet

public void UploadPackageItem(FileContent fileContent)

        {         

 

         if (fileContent == null)

                    return;

        var filePath = String.Format(“{0}/{1}”, Config.UploadLocation,fileContent.Id);

 

        var dir = Path.GetDirectoryName(filePath);

 

        if (!Directory.Exists(directory))

            Directory.CreateDirectory(directory);

 

        using (var outputStream = new FileStream(filePath, FileMode.Create))

                {

                    fileContent.file.CopyTo(outputStream);

                }           

 

        }

 

Client side end point configuration

Code Snippet
  1. <system.serviceModel>
  2.         <client>
  3.           <endpoint name=”FileUpload” address=”net.tcp://localhost:19999/FileUploadService.svc/FileUpload” binding=”netTcpBinding”
  4.                 behaviorConfiguration=”LargeFileServiceBehavior” bindingConfiguration=”NetTcpBinding_FileUpload” contract=”FileUploadSerivce.Services.IFileUploadService”/>
  5.         </client>
  6.     <bindings>
  7.       <netTcpBinding>
  8.         <binding name=”NetTcpBinding_FileUpload” transferMode=”Streamed”  maxReceivedMessageSize=”9223372036854775807″  receiveTimeout=”01:00:00″ sendTimeout=”00:10:00″  closeTimeout =”0:01:00″ openTimeout=”0:01:00″>
  9.         </binding>
  10.       </netTcpBinding>
  11.     </bindings>
  12.   </system.serviceModel>

 

Client implementation

Code Snippet

public void UploadFileToService(Int32 fileId)

   {

   var fileContent = new FileContent()

            {

                fileId = fileId,

                Content = (new FileStream(string.Format(“{0}/{1}”, Config.Package_UploadLocation, fileId), FileMode.Open, FileAccess.Read)),

               

            };

 

            serviceProxyProvider.GetService<IFileUploadService>(“FileUpload”).ExecuteCommand(s =>

            {

                s.UploadFile(fileContent);

            });

        }

 

You might also like