|
| 1 | +--- |
| 2 | +title: ".NET 7 breaking change: BrotliStream no longer allows undefined CompressionLevel values" |
| 3 | +description: Learn about the .NET 7 breaking change in core .NET libraries where the BrotliStream constructors no longer allow undefined CompressionLevel values. |
| 4 | +ms.date: 11/18/2022 |
| 5 | +--- |
| 6 | +# BrotliStream no longer allows undefined CompressionLevel values |
| 7 | + |
| 8 | +The <xref:System.IO.Compression.BrotliStream> constructors that take a <xref:System.IO.Compression.CompressionLevel> argument no longer allow values that aren't defined in the <xref:System.IO.Compression.CompressionLevel> enumeration. If you pass an invalid value, an <xref:System.ArgumentException> is thrown. |
| 9 | + |
| 10 | +## Previous behavior |
| 11 | + |
| 12 | +<xref:System.IO.Compression.BrotliStream> allowed you to pass an arbitrary compression level to the constructor by casting the desired level directly to <xref:System.IO.Compression.CompressionLevel>. For example: |
| 13 | + |
| 14 | +```csharp |
| 15 | +BrotliStream brotli = new BrotliStream(baseStream, |
| 16 | + (CompressionLevel)5); // Use level 5 |
| 17 | +``` |
| 18 | + |
| 19 | +However, if an arbitrary level was provided, that was passed through as-is to the underlying library, resulting in inconsistent and potentially unexpected behavior. |
| 20 | + |
| 21 | +## New behavior |
| 22 | + |
| 23 | +<xref:System.IO.Compression.BrotliStream> only allows the values defined in <xref:System.IO.Compression.CompressionLevel>. If you pass an undefined value to the constructor, an <xref:System.ArgumentException> is thrown. |
| 24 | + |
| 25 | +## Version introduced |
| 26 | + |
| 27 | +.NET 7 |
| 28 | + |
| 29 | +## Type of breaking change |
| 30 | + |
| 31 | +This change can affect [binary compatibility](../../categories.md#binary-compatibility). |
| 32 | + |
| 33 | +## Reason for change |
| 34 | + |
| 35 | +The purpose of the <xref:System.IO.Compression.CompressionLevel> enumeration is to let developers use compression algorithms without needing to understand the meaning of their tuning parameters. |
| 36 | + |
| 37 | +If an arbitrary level was provided, that was passed through as-is to the underlying library, resulting in inconsistent and potentially unexpected behavior. With this change, the behavior is aligned with other compression streams, for example, <xref:System.IO.Compression.DeflateStream>. |
| 38 | + |
| 39 | +With the new tuning of the <xref:System.IO.Compression.CompressionLevel> values and the addition of <xref:System.IO.Compression.CompressionLevel.SmallestSize?displayProperty=nameWithType>, it's now possible to have a variety of trade-offs in the compression algorithms. Users can continue to rely on <xref:System.IO.Compression.CompressionLevel> values as being abstractions of such trade-offs. |
| 40 | + |
| 41 | +## Recommended action |
| 42 | + |
| 43 | +If you were relying on passing undefined values as the <xref:System.IO.Compression.CompressionLevel>, revisit your use case and decide which documented value is the most optimal for it. |
| 44 | + |
| 45 | +## Affected APIs |
| 46 | + |
| 47 | +- <xref:System.IO.Compression.BrotliStream.%23ctor(System.IO.Stream,System.IO.Compression.CompressionLevel,System.Boolean)> |
| 48 | +- <xref:System.IO.Compression.BrotliStream.%23ctor(System.IO.Stream,System.IO.Compression.CompressionLevel)> |
0 commit comments