Code Stucks at Microsoft.SharePoint.Client.File.OpenBinaryDirect Method

%3CLINGO-SUB%20id%3D%22lingo-sub-2612666%22%20slang%3D%22en-US%22%3ECode%20Stucks%20at%20Microsoft.SharePoint.Client.File.OpenBinaryDirect%20Method%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2612666%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20download%20a%20file%20using%20ServeRelativeUrl%26nbsp%3B%20by%26nbsp%3BOpenBinaryDirect%20Method%20and%20it%20works%20fine.%20But%20often%2C%26nbsp%3B%26nbsp%3Birrespective%20of%20size%2C%20extension%20or%20any%20other%20file%20metadata%20it%20just%20get%20stucks.%3CBR%20%2F%3E%3CBR%20%2F%3EPlease%20see%20the%20code%20below%2C%20it%20gets%20stucks%20at%20either%20of%20the%20bolded%20lines%20(more%20often%20at%20ExecuteQuery())%20and%20throws%20an%20operation%20timeout%20error%2C%20later%20I%20tried%20to%20give%20timeouts%20(shown%20in%20italic)%20but%20then%20it%20got%20stuck%20for%20infinite%20time%20until%20the%20main%20thread%20got%20killed.%3CBR%20%2F%3E%3CBR%20%2F%3EclientContext.RequestTimeout%20%3D%20-1%3B%3CBR%20%2F%3EFileInformation%20fileInfo%20%3D%20Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext%2C%20file.ServerRelativeUrl)%3B%3CBR%20%2F%3EclientContext.ExecuteQuery()%3B%3C%2FP%3E%3CP%3Eusing%20(var%20fileStream%20%3D%20new%20FileStream(location%2C%20FileMode.Create))%3CBR%20%2F%3E%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20fileInfo.Stream.WriteTimeout%20%3D%20-1%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20fileInfo.Stream.CopyTo(fileStream)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3EfileInfo.Stream.Dispose()%3B%3CBR%20%2F%3E%3CBR%20%2F%3EI%20have%20attached%20a%20snip%20of%20the%20explorer%20when%20my%20code%20got%20stuck%20at%20this%20particular%20file%2C%20it's%20size%20was%200kb%20after%20the%20main%20thread%20got%20killed.%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ECan%20anyone%20please%20help%20me%20out%20with%20this%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

I am trying to download a file using ServeRelativeUrl  by OpenBinaryDirect Method and it works fine. But often,  irrespective of size, extension or any other file metadata it just get stucks.

Please see the code below, it gets stucks at either of the bolded lines (more often at ExecuteQuery()) and throws an operation timeout error, later I tried to give timeouts (shown in italic) but then it got stuck for infinite time until the main thread got killed.

clientContext.RequestTimeout = -1;
FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, file.ServerRelativeUrl);
clientContext.ExecuteQuery();

using (var fileStream = new FileStream(location, FileMode.Create))
{

                    fileInfo.Stream.WriteTimeout = -1;

                    fileInfo.Stream.CopyTo(fileStream);
}
fileInfo.Stream.Dispose();

I have attached a snip of the explorer when my code got stuck at this particular file, it's size was 0kb after the main thread got killed.


Can anyone please help me out with this?

1 Reply

@akshar_ganatra 

 

If you create the FileInformation object in a using block, that should fix things for you.