Skip to content

.Net 6/7 IAsyncDisposable uses wrong DisposeAsync method #730

Closed

Description

@roddone

Steps to reproduce

  1. Create a project targeting .net 6 or higher
  2. use ZipOutputStream with "await using"
  3. Or call directly .DisposeAsync() on previously created ZipOutputStream

Expected behavior

When Disposing, or calling .DisposeAsync(), it uses DeflaterOutputStream.DisposeAsync()

Actual behavior

When Disposing, it uses the base class Stream.DisposeAsync() instead of DeflaterOutputStream.DisposeAsync() method.
This is caused by a compilation directive #if NETSTANDARD2_1 that does not take in consideration recent .net versions (.Net 6 and 7, and probably 5, I have not tested this version)
image

Version of SharpZipLib

  • Compiled from source (latest commit on master)

full sample to reproduce (must create a .net 6 project) :

using ICSharpCode.SharpZipLib.Zip;
using System.Text;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", async (HttpContext context) =>
{
	await using (var outputStream = new MemoryStream())
	{
		await using (var stream = new ZipOutputStream(outputStream))
		{
			//add file without compression
			await stream.PutNextEntryAsync(new ZipEntry("test.txt") { CompressionMethod = CompressionMethod.Stored });

			var bytes = Encoding.UTF8.GetBytes("ah ah !");
			await stream.WriteAsync(bytes);

			await stream.FinishAsync(CancellationToken.None);
			outputStream.Position = 0;
			await outputStream.CopyToAsync(context.Response.Body);
		}
	}
});

app.Run();

Activity

  1. piksel commented on Feb 23, 2022

    @piksel
    Member

    Good catch, it should be NETSTANDARD2_1_OR_GREATER instead. It shouldn't happen unless you change the TargetFrameworks in the .csproj though? Right now we are only targeting netstandard2.0, netstandard2.1 and net45, so it shouldn't be a problem.

  2. roddone commented on Feb 23, 2022

    @roddone
    Author

    It also happens when you generate the nuget package from source and reference it in a .net 6 project. That's how I spotted the issue.
    To find what caused it, I've added the .net6.0 in targetframeworks (for testing only) ;)

  3. piksel commented on May 24, 2022

    @piksel
    Member

    When running the example you provided above, I don't get any errors and it seems to work fine.
    The compiler directives are interpreted when you are building the library, and since the targets does not include net6.0 you should get the library targeting netstandard2.1 when using it with a NET 6.0 project.

  4. piksel commented on May 24, 2022

    @piksel
    Member

    In any case, this is fixed in #755 that actually adds support for .NET 6.0 target.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions