Hello everybody, I wrote a file download WCF service to download ten video files each of 2.5 GB using basic http binding hosted on a manage application from sql server 2008 database.

Below is WCF Service code for Download Video file:

////This download service method to download ten video files of 2.5 GB

public Stream DownloadObject(ObjectIdentifier id)

{
int sequences = 0;
using (SqlConnection connection = GetConnection())
{
connection.Open();
using (SqlCommand cmd = connection.CreateCommand())
{
cmd.CommandText = sql query to get a sequence value
cmd.Parameters.AddWithValue("@Id", id.ObjectId);
sequences = (int)cmd.ExecuteScalar();
}
}
if (sequences == 1)
{
SqlConnection connection = GetConnection();
connection.Open();
SqlTransaction transaction = connection.BeginTransaction();

//Get the file ourselves.
FileStreamAccess fileStreamAccess = new FileStreamAccess(connection, id.ObjectId, transaction);
if (fileStreamAccess.IsValid)
{
SqlFileStream strm = fileStreamAccess.GetStream(FileAccess.Read);
return strm;
}
else
{
Stream emptystream = null;
return emptystream;
}
}
}
}


FileStreamAccess(SqlConnection cn, Guid LargeObjectId, SqlTransaction transaction)
{
using (SqlCommand cmd = cn.CreateCommand())
{
cmd.Transaction = transaction;
cmd.CommandText = sql query to get the path and transaction context
cmd.Parameters.AddWithValue("Id", FileObjectId);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
this.PathName = reader.GetString(0);
this.Context = (byte[])reader.GetValue(1);
}
reader.Close();
}

}

SqlFileStream GetStream(FileAccess access)
{ return new SqlFileStream(PathName, Context, access); }


WCF Service Config:

<basicHttpBinding>

<binding name="largeFileStreaming" messageEncoding="Mtom" maxReceivedMessageSize="21474836470" transferMode="Streamed" closeTimeout="10:10:00" openTimeout="10:10:00" receiveTimeout="00:30:00" sendTimeout="10:10:00">

<readerQuotas maxDepth="2147483647"

maxStringContentLength="2147483647"

maxArrayLength="2147483647"

maxBytesPerRead="2147483647"

maxNameTableCharCount="2147483647"

/>

</binding>

</basicHttpBinding>





Client Web Config :

<binding name="LargeFileStreaming" messageEncoding="Mtom"

maxBufferSize="2147483647" maxBufferPoolSize="21474836470" maxReceivedMessageSize="21474836470" transferMode="Streamed" closeTimeout="10:10:00" openTimeout="10:10:00" receiveTimeout="00:30:00" sendTimeout="10:10:00">



Error Occurred at Client End:

System.IO.IOException: An exception has been thrown when reading the stream. at System.ServiceModel.Dispatcher.StreamFormatter.MessageBodyStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent) at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root) at MS.Internal.LoadedOrUnloadedOperation.DoWork() at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks() at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget) at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)

System.ServiceModel.CommunicationException: An error (Unable to read data from the transport connection: The connection was closed.) occurred while transmitting data over the HTTP channel. at System.ServiceModel.Channels.HttpInput.WebResponseHttpInput.WebResponseInputStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.ServiceModel.Channels.MaxMessageSizeStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.ServiceModel.Channels.DetectEofStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Xml.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Xml.DelimittedStreamReader.Read(DelimittedReadStream caller, Byte[] buffer, Int32 offset, Int32 count) at System.Xml.DelimittedStreamReader.DelimittedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Xml.XmlMtomReader.XopIncludeReader.ReadContentAsBase64(Byte[] buffer, Int32 offset, Int32 count) at System.Xml.XmlMtomReader.ReadContentAsBase64(Byte[] buffer, Int32 offset, Int32 count) at System.ServiceModel.Dispatcher.StreamFormatter.MessageBodyStream.Read(Byte[] buffer, Int32 offset, Int32 count)

System.IO.IOException: Unable to read data from the transport connection: The connection was closed. at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.ServiceModel.Channels.PreReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.ServiceModel.Channels.HttpInput.WebResponseHttpInput.WebResponseInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)

The handle is invalid. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.ReadCore(Byte[] buffer, Int32 offset, Int32 count) at System.IO.FileStream.Read(Byte[] array, Int32 offset, Int32 count) at System.Data.SqlTypes.SqlFileStream.Read(Byte[] buffer, Int32 offset, Int32 count)