From 3b0518a2a75203e008b5e2364a0b587692dd8f01 Mon Sep 17 00:00:00 2001 From: thelocalsim Date: Fri, 28 Aug 2026 09:42:42 +0530 Subject: [PATCH] Only set AutomaticDecompression when the platform supports it Constructing an EsiClient on Blazor WebAssembly threw "One or more errors occurred. (Operation is not supported on this platform.)" before any request was made. The default handler set HttpClientHandler.AutomaticDecompression unconditionally. On the browser runtime that property is annotated [UnsupportedOSPlatform("browser")] and the underlying BrowserHttpHandler throws PlatformNotSupportedException from both the getter and the setter, because the fetch API performs content decoding itself and exposes no way to configure it. Guard the assignment with HttpClientHandler.SupportsAutomaticDecompression, which the browser handler defines as a compile-time constant false and which returns true on every other supported platform. The property is a plain bool getter that never throws, and it is present in every target framework this project builds for (netstandard2.0, net462 through net48, netcoreapp3.1, net6.0 and net7.0), so no additional conditional compilation is needed. Behaviour is unchanged everywhere decompression is supported: the existing #if NET split between DecompressionMethods.All and GZip|Deflate is preserved. On Blazor WebAssembly the client now constructs successfully and the browser handles gzip/deflate/brotli transparently. The handler construction moves into a small private factory so that a handler is still only allocated when no HttpClient is supplied by the caller. Fixes #77 Co-Authored-By: Claude Opus 5 (1M context) --- ESI.NET/EsiClient.cs | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/ESI.NET/EsiClient.cs b/ESI.NET/EsiClient.cs index 5b00fe4..aa7f45a 100644 --- a/ESI.NET/EsiClient.cs +++ b/ESI.NET/EsiClient.cs @@ -21,17 +21,7 @@ public class EsiClient : IEsiClient public EsiClient(IOptions _config, HttpClient _client = null) { config = _config.Value; - client = _client ?? new HttpClient(new HttpClientHandler - { - - -// Switch to All which adds brotli encoding for .net core due to https://github.com/ccpgames/sso-issues/issues/81 -#if NET - AutomaticDecompression = DecompressionMethods.All -#else - AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate -#endif - }); + client = _client ?? new HttpClient(CreateDefaultHandler()); // Enforce user agent value if (string.IsNullOrEmpty(config.UserAgent)) @@ -143,6 +133,32 @@ public void SetCharacterData(AuthorizedCharacterData data) public void SetIfNoneMatchHeader(string eTag) => EsiRequest.ETag = eTag; + + /// + /// Creates the used when no is supplied. + /// + /// + /// Automatic decompression is only configured when the handler reports support for it. + /// On Blazor WebAssembly the underlying browser handler throws + /// from the setter, because the browser's fetch + /// API performs content decoding itself. See https://github.com/seraphx2/ESI.NET/issues/77. + /// + private static HttpClientHandler CreateDefaultHandler() + { + var handler = new HttpClientHandler(); + + if (handler.SupportsAutomaticDecompression) + { + // Switch to All which adds brotli encoding for .net core due to https://github.com/ccpgames/sso-issues/issues/81 +#if NET + handler.AutomaticDecompression = DecompressionMethods.All; +#else + handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; +#endif + } + + return handler; + } } public interface IEsiClient