2
votes

I am using following code to send a file to an external service.. It works for small files, but throws IOException for big files..

FileStream newStream = File.OpenRead(_fullFilePath);
MemoryStream storeStream = new MemoryStream();
storeStream.SetLength(newStream.Length);
newStream.Read(storeStream.GetBuffer(), 0, (int)newStream.Length);
storeStream.Flush();
//send data to external service
newStream.Close();
storeStream.Close();

It seems like something to do with the way I call make the stream.. but I couldnt find the exact solution.. Error details is given below

System.Net.WebException: The request was aborted: The request was canceled. ---> System.IO.IOException: Cannot close stream until all bytes are written.
  at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)
  --- End of inner exception stack trace ---
  at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)
  at System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
  at System.Net.ConnectStream.Dispose(Boolean disposing)
  at System.IO.Stream.Close()
  at DotNetOpenAuth.Messaging.MessagingUtilities.PostMultipartNoGetResponse(HttpWebRequest request, IDirectWebRequestHandler requestHandler, IEnumerable`1 parts) in c:\Users\andarno\git\dotnetopenid\src\DotNetOpenAuth\Messaging\MessagingUtilities.cs:line 243
  at DotNetOpenAuth.OAuth.ChannelElements.OAuthChannel.InitializeRequestAsAuthHeader(IDirectedProtocolMessage requestMessage) in c:\Users\andarno\git\dotnetopenid\src\DotNetOpenAuth\OAuth\ChannelElements\OAuthChannel.cs:line 373
  at DotNetOpenAuth.OAuth.ChannelElements.OAuthChannel.CreateHttpRequest(IDirectedProtocolMessage request) in c:\Users\andarno\git\dotnetopenid\src\DotNetOpenAuth\OAuth\ChannelElements\OAuthChannel.cs:line 214
  at DotNetOpenAuth.OAuth.ConsumerBase.PrepareAuthorizedRequest(MessageReceivingEndpoint endpoint, String accessToken, IEnumerable`1 binaryData) in c:\Users\andarno\git\dotnetopenid\src\DotNetOpenAuth\OAuth\ConsumerBase.cs:line 132
  at Visual.ApiProvider.DoRequest(MessageReceivingEndpoint message, List`1 parameters) in C:\Users\Nick Bruun\Code\drei\23-api-dotnet\src\Implementations\ApiProvider.cs:line 137
  at Visual.PhotoService.Upload(String filename, String fileContentType, Stream filestream, Nullable`1 userId, Nullable`1 albumId, String title, String description, String tags, Nullable`1 publish) in C:\Users\Nick Bruun\Code\drei\23-api-dotnet\src\Implementations\PhotoService.cs:line 294
  at usercontrols_UploadVideos.Upload23Video(Object parameters) in e:\Projects\videoWeb\usercontrols\UploadVideos.ascx.cs:line 138

New code with Using

using (FileStream newStream = File.OpenRead(_fullFilePath))
{
    newStream.Flush();
    using (MemoryStream storeStream = new MemoryStream())
    {
        storeStream.SetLength(newStream.Length);
        newStream.Read(storeStream.GetBuffer(), 0, (int)newStream.Length);
        storeStream.Flush();
        newStream.Close();
        //call to webservice
        storeStream.Close();
    }
}
1
You should be wrapping the creation of the streams in using statements. - Oded
Disabled Keep Alive and added FileStream and MemoryStream inside Using.. but still getting the error.. Max request length is very high value.. - Ansar Muhammad
why are you buffering the potentially very large file in memory? Where are you "sending the data to an external device"? - BrokenGlass
The file is being sent to a video hosting service 23video.com using their API.. 23developer.com/api/photo-upload .. The webservice has a .NET wrapper github.com/23/23-api-dotnet which accepts the file as a Stream.. - Ansar Muhammad

1 Answers

3
votes

The reason you're getting this is that the destination isn't configured to allow for files that big. You have to adjust the config of the destination's Max Request length.