bezzad / Downloader

Fast, cross-platform and reliable multipart downloader with asynchronous progress events for .NET applications.
MIT License
1.25k stars 193 forks source link

Cannot serialize download package after cancelled or failed. #129

Closed zaevi closed 1 year ago

zaevi commented 1 year ago

Hi, I want to serialize download package when downloading is cancelled or failed, so that I can resume downloading next time. But when serializing it throws ObjectDisposedException:

// .net7 top-level program
using Downloader;

var downloader = new DownloadService();

var times = 3;
downloader.ChunkDownloadProgressChanged += (_, e) =>
{
    if (times-- == 0) downloader.CancelAsync();
};

downloader.DownloadFileCompleted += (_, e) =>
{
    var package = e.UserState as DownloadPackage;
    if(package!.Status != DownloadStatus.Completed)
        JsonSerializer.Serialize(package!); // throws ObjectDisposedException
};

var url = "http://cachefly.cachefly.net/10mb.test";
await downloader.DownloadFileTaskAsync(url, "./download.test");
bezzad commented 1 year ago

Please use it like this:

var package = downloader.Package;

or

var package = (sender as DownloadService).Package;

sender object is the same with _ in your codes ;)

zaevi commented 1 year ago

Please use it like this:

var package = downloader.Package;

or

var package = (sender as DownloadService).Package;

sender object is the same with _ in your codes ;)

It's same reference and it still throws the exception. Seems package.Storage has been disposed so it can't be serialized.

bezzad commented 1 year ago

This issue was fixed with the test. Please test the Downloader new version 3.0.3. If you have the problem again, so re-open this issue. Thank you for reporting and the pull request.