Forum Discussion

roman prog 89 level_'s avatar
roman prog 89 level_
Explorer | Level 3
4 years ago
Solved

How to continue if the connection is interrupted.

Hi everyone.
File unload question.
How to continue if the connection is interrupted.
An example of my code.

 

 

using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
int numChunks = (int)Math.Ceiling((double)stream.Length / chunkSizeByte);
byte[] buffer = new byte[chunkSizeByte];
string sessionId = null;

for (var idx = 0; idx < numChunks; idx++)
{

var byteRead = stream.Read(buffer, 0, chunkSizeByte);

using (MemoryStream memStream = new MemoryStream(buffer, 0, byteRead))
{
if (idx == 0)
{
try
{
var result = await dbx2.Files.UploadSessionStartAsync(close:false , body: memStream);
sessionId = result.SessionId;
Log(DateTime.Now + " - TRYING - " + i + " - FRAGMENT FIRST - " + idx);

}
catch (Exception ui)
{
Log(DateTime.Now + " - TRYING - " + i + " - FRAGMENT FIRST ERROR - " + ui.Message);
}
}
else
{
ulong size = chunkSize * (ulong)idx;
UploadSessionCursor cursor = new UploadSessionCursor(sessionId, size);

if (idx == numChunks - 1)
{
try
{
await dbx2.Files.UploadSessionFinishAsync(cursor, new CommitInfo(folder + "/" + Date + filenamepath), memStream);
Log(DateTime.Now + " - TRYING - " + i + " - FRAGMENT LAST - " + idx);
}
catch (Exception gh)
{
Log(DateTime.Now + " - TRYING - " + i + " - FRAGMENT LAST ERROR - " + gh.Message);
}
}
else
{
try
{
await dbx2.Files.UploadSessionAppendV2Async(cursor, close:false, body: memStream); // if there is an upload error
Log(DateTime.Now + " - TRYING - " + i + " - FRAGMENT PART - " + idx);
}
catch (Exception yt)
{
Log(yt.Message + " - - - " + yt.InnerException + " - - - " + yt.TargetSite);

System.Threading.Thread.Sleep(pause);

try
{
await dbx2.Files.UploadSessionAppendV2Async(cursor, close: false, body: memStream);
}
catch (Exception er)
{
Log("TRAYING ERROR - " + " - " + er.Message);
}
}
}
}
}
}
}

  • Greg-DB's avatar
    Greg-DB
    4 years ago

    The "Access to a closed stream is not possible" error message doesn't appear to be coming from the Dropbox API itself, so I can't offer too much help with that in particular. That said, it's likely referring to that "memStream" object. It looks like that's closed, since it was used in the first attempt, so you'd need to make a new one for the new attempt.

6 Replies

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    4 years ago

    I see you're using upload sessions to upload files by doing so in multiple pieces. With this functionality, you can catch and handle any exceptions that may occur, and then resume the process by picking up with whichever upload session call you last made.

     

    For instance, if a particular call, such as UploadSessionStartAsync or UploadSessionAppendV2Async, for instance, fails due to a network issue, you can have your code catch the exception and then retry that particular call.

     

    Note that in some cases, depending on the exact nature and timing of the disconnection, it's possible for the Dropbox servers to receive the complete call, without your client receiving the response for that call. In that kind of case, the client and server may then disagree on the current offset. If that happens, on the next call for the upload session, the client will get a UploadSessionLookupError.IncorrectOffset with the UploadSessionOffsetError containing the CorrectOffset that it can use to resume the upload session at the correct offset.

  • roman prog 89 level_'s avatar
    roman prog 89 level_
    Explorer | Level 3
    4 years ago

    yes, I am doing error trapping.

    But when I try to re-upload it doesn't work. although the connection has already been restored. I'm trying it on the bench.

    It is noticeable that he does not even try to unload.

     

    else
    {
    try
    {
    await dbx2.Files.UploadSessionAppendV2Async(cursor, close:false, body: memStream); // if there is an upload error
    Log(DateTime.Now + " - TRYING - " + i + " - FRAGMENT PART - " + idx);
    }
    catch (Exception yt)
    {
    Log(yt.Message + " - - - " + yt.InnerException + " - - - " + yt.TargetSite);

    System.Threading.Thread.Sleep(pause);

    try
    {
    await dbx2.Files.UploadSessionAppendV2Async(cursor, close: false, body: memStream); //there is no reloading at this point.
    }
    catch (Exception er)
    {
    Log("TRAYING ERROR - " + " - " + er.Message);
    }
    }
    }

     

     

     

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    4 years ago

    Can you elaborate on what you mean when you say "it doesn't work"? What unexpected error, output, or behavior are you getting? I recommend stepping through with the debugger first to see what is being executed and when to understand what is happening.

  • roman prog 89 level_'s avatar
    roman prog 89 level_
    Explorer | Level 3
    4 years ago

    I'll tell you more.
    I'm testing the problem with unloading, in the debugger, manually turning off the Internet.
    When an error occurs in UploadSessionAppendV2Async, the message is: "An error occurred while copying content to the stream. - - - System.IO.IOException: Cannot write data to transport connection: The remote host forcibly terminated the existing connection. ---> System .Net.Sockets.SocketException: The remote host forcibly terminated an existing connection"

     

    Next, I intercept the error, try to re-upload through
    UploadSessionAppendV2Async but getting error: "Access to a closed stream is not possible."

     

    else
    {
    try
    {
    await dbx2.Files.UploadSessionAppendV2Async(cursor, close:false, body: memStream); // if there is an upload error - An error occurred while copying content to the stream.
    Log(DateTime.Now + " - TRYING - " + i + " - FRAGMENT PART - " + idx);
    }
    catch (Exception yt)
    {
    Log(yt.Message + " - - - " + yt.InnerException + " - - - " + yt.TargetSite);

    System.Threading.Thread.Sleep(pause);

    try
    {
    await dbx2.Files.UploadSessionAppendV2Async(cursor, close: false, body: memStream); //there is no reloading at this point. - Access to a closed stream is not possible.
    }
    catch (Exception er)
    {
    Log("TRAYING ERROR - " + " - " + er.Message);
    }
    }
    }

     

    I don’t understand why the stream is closing, how can I connect to it again?

     

     

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    4 years ago

    The "Access to a closed stream is not possible" error message doesn't appear to be coming from the Dropbox API itself, so I can't offer too much help with that in particular. That said, it's likely referring to that "memStream" object. It looks like that's closed, since it was used in the first attempt, so you'd need to make a new one for the new attempt.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!