0

We are using Box.V2.Sdk to upload files to box.com using the following code

Function UploadToBox(ByVal attachedFilename As String, ByVal stream As System.IO.Stream) As Boolean

    Dim clientID As String
    Dim clientSecret As String
    Dim oldRefreshToken As String
    Dim newToken As BoxApi.V2.Authentication.OAuth2.OAuthToken

    clientID = "your client id"
    clientSecret = "you client secret"

    Dim tokenProvider As New TokenProvider(clientID, clientSecret)

    '''' Reading Refresh token from the file
    Dim streamReader As StreamReader
    streamReader = System.IO.File.OpenText(Server.MapPath("~\\Box\\BoxApiRefreshToken.txt"))
    oldRefreshToken = streamReader.ReadToEnd()
    streamReader.Close()

    newToken = tokenProvider.RefreshAccessToken(oldRefreshToken)
    Dim boxManager As New BoxManager(newToken.AccessToken)

    '''' Writing the new Refresh token to the file
    Dim streamWriter As New StreamWriter(Server.MapPath("~\\Box\\BoxApiRefreshToken.txt"))
    streamWriter.Write(newToken.RefreshToken)
    streamWriter.Close()

    Dim rootFolder As Folder

    rootFolder = boxManager.GetFolder(Folder.Root)

    boxManager.CreateFile(rootFolder, attachedFilename, ConvertStreamToByteArray(stream))

    Return True

End Function 

mentioned here http://www.codeproject.com/Tips/769414/Uploading-files-using-Box-API

The UploadToBox method is in a loop and called for every file we need to upload. However, after few uploads, we are getting the error

The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.

Intuitively it appears that the connection to box is getting closed. But from the code above, we dont know which part is trying to create the connection, and if its closed then create a new one. Its fairly easy with c# SqlConnection to check if connection is open or closed.

Please suggest your thoughts.

blue piranha
  • 3,706
  • 13
  • 57
  • 98

1 Answers1

0

This issue sounds similar to one that was found in the officially supported sdk and fixed: https://github.com/box/box-windows-sdk-v2

Have you tried using that one?

The original issue for that was that the connection was getting closed due to the default timeout expiring before the upload was complete. Are the uploads that are failing larger than the ones that are succeeding? The SDK you're using uses RestSharp underneath the covers, so you may need to take a look at the source to see whether or not the timeout can be configured.

HTH

letstango
  • 789
  • 6
  • 14