Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,36 @@ public async Task TestRefreshUsesSuppliedHttpClient()
Assert.AreEqual(1, refreshRequests, "The refresh request should flow through the supplied HttpClient.");
}

/// <summary>
/// Tests that upload disposes the stream by default.
/// </summary>
/// <returns>The <see cref="Task" />.</returns>
[TestMethod]
public async Task TestUploadDisposesStreamByDefault()
{
var stream = GetStream("abc");
await client.Files.UploadAsync(testingPath + "/Foo.txt", body: stream);

Assert.ThrowsException<ObjectDisposedException>(() => stream.Position);
}

/// <summary>
/// Tests that upload keeps the stream open when DisposeUploadStream is false.
/// </summary>
/// <returns>The <see cref="Task" />.</returns>
[TestMethod]
public async Task TestUploadKeepsStreamOpenWhenConfigured()
{
var config = new DropboxClientConfig { DisposeUploadStream = false };
var keepOpenClient = new DropboxClient(userAccessToken, config);

var stream = GetStream("abc");
await keepOpenClient.Files.UploadAsync(testingPath + "/Bar.txt", body: stream);

stream.Seek(0, SeekOrigin.Begin);
Assert.AreEqual(3, stream.Length);
}

/// <summary>
/// Test get metadata.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions dropbox-sdk-dotnet/Dropbox.Api/DropboxClientConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,12 @@ public DropboxClientConfig(string userAgent, int maxRetriesOnError)
/// http client with a longer timeout (480 seconds) will be created.
/// </summary>
public HttpClient LongPollHttpClient { get; set; }

/// <summary>
/// Gets or sets a value indicating whether an upload stream is disposed by
/// the SDK after the request completes. Default is <c>true</c> for
/// backwards compatibility; set to <c>false</c> to keep the stream open.
/// </summary>
public bool DisposeUploadStream { get; set; } = true;
}
}
18 changes: 15 additions & 3 deletions dropbox-sdk-dotnet/Dropbox.Api/DropboxRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,10 @@ private async Task<Result> RequestJsonStringWithRetry(
}
finally
{
body?.Dispose();
if (this.options.DisposeUploadStream)
{
body?.Dispose();
}
}
}

Expand Down Expand Up @@ -934,7 +937,8 @@ public DropboxRequestHandlerOptions(DropboxClientConfig config, string oauth2Acc
DefaultApiContentDomain,
DefaultApiNotifyDomain,
config.HttpClient,
config.LongPollHttpClient)
config.LongPollHttpClient,
config.DisposeUploadStream)
{
}

Expand All @@ -958,6 +962,7 @@ public DropboxRequestHandlerOptions(DropboxClientConfig config, string oauth2Acc
/// http client will be created.</param>
/// <param name="longPollHttpClient">The custom http client for long poll. If not provided, a default
/// http client with longer timeout will be created.</param>
/// <param name="disposeUploadStream">Whether to dispose the upload stream after the request.</param>
public DropboxRequestHandlerOptions(
string oauth2AccessToken,
string oauth2RefreshToken,
Expand All @@ -970,7 +975,8 @@ public DropboxRequestHandlerOptions(
string apiContentHostname,
string apiNotifyHostname,
HttpClient httpClient,
HttpClient longPollHttpClient)
HttpClient longPollHttpClient,
bool disposeUploadStream = true)
{
var type = typeof(DropboxRequestHandlerOptions);
#if PORTABLE40
Expand All @@ -987,6 +993,7 @@ public DropboxRequestHandlerOptions(

this.HttpClient = httpClient;
this.LongPollHttpClient = longPollHttpClient;
this.DisposeUploadStream = disposeUploadStream;
this.OAuth2AccessToken = oauth2AccessToken;
this.OAuth2RefreshToken = oauth2RefreshToken;
this.OAuth2AccessTokenExpiresAt = oauth2AccessTokenExpiresAt;
Expand Down Expand Up @@ -1041,6 +1048,11 @@ public DropboxRequestHandlerOptions(
/// </summary>
public HttpClient LongPollHttpClient { get; private set; }

/// <summary>
/// Gets a value indicating whether to dispose the upload stream after the request.
/// </summary>
public bool DisposeUploadStream { get; private set; }

/// <summary>
/// Gets the user agent string.
/// </summary>
Expand Down
Loading