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
- <service name=”LargeFileService.Service.LargeFileService” behaviorConfiguration=”LargeFileServiceBehavior”>
- <host>
- <!– Create base address–>
- <baseAddresses>
- <add baseAddress=”net.tcp://localhost:19999/LargeFileService.svc”/>
- </baseAddresses>
- </host>
- <!– Create end point, pleaes note binding configuration NetTcpBinding_FileUpload–>
- <endpoint address=”/FileUpload” binding=”netTcpBinding” bindingConfiguration=”NetTcpBinding_FileUpload”
- contract=”LargeFileService.Services.App.ILargeFileService”/>
- <!– Metadata Endpoints –>
- <!– The Metadata Exchange endpoint is used by the service to describe itself to clients. –>
- <!– This endpoint does not use a secure binding and should be secured or removed before deployment –>
- <endpoint address=”mex” binding=”mexTcpBinding” contract=”IMetadataExchange”/>
- </service>
Large file binding configuration
- <bindings>
- <netTcpBinding>
- <binding name=”NetTcpBinding_FileUpload” transferMode=”Streamed”
- maxReceivedMessageSize=”9223372036854775807″ receiveTimeout=”01:00:00″
- sendTimeout=”00:10:00″ closeTimeout =”0:01:00″ openTimeout=”0:01:00″>
- </binding>
- </netTcpBinding>
- </bindings>
Service Contract
- [OperationContract]
- void UploadFile(FileContent item);
Service Implementation
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
- <system.serviceModel>
- <client>
- <endpoint name=”FileUpload” address=”net.tcp://localhost:19999/FileUploadService.svc/FileUpload” binding=”netTcpBinding”
- behaviorConfiguration=”LargeFileServiceBehavior” bindingConfiguration=”NetTcpBinding_FileUpload” contract=”FileUploadSerivce.Services.IFileUploadService”/>
- </client>
- <bindings>
- <netTcpBinding>
- <binding name=”NetTcpBinding_FileUpload” transferMode=”Streamed” maxReceivedMessageSize=”9223372036854775807″ receiveTimeout=”01:00:00″ sendTimeout=”00:10:00″ closeTimeout =”0:01:00″ openTimeout=”0:01:00″>
- </binding>
- </netTcpBinding>
- </bindings>
- </system.serviceModel>
Client implementation
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);
});
}