Skip to content

Commit

Permalink
#823 detect Blazor w/o try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
Todd committed Aug 2, 2024
1 parent 221ce35 commit 1bb423a
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Flurl.Http/Configuration/FlurlClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;

namespace Flurl.Http.Configuration
{
Expand Down Expand Up @@ -41,6 +42,14 @@ public static class FlurlClientFactoryExtensions
/// </summary>
public class DefaultFlurlClientFactory : IFlurlClientFactory
{
// cached Blazor/WASM check (#543, #823)
private readonly bool _isBrowser =
#if NET
OperatingSystem.IsBrowser();
#else
false;
#endif

/// <inheritdoc />
public virtual HttpClient CreateHttpClient(HttpMessageHandler handler) {
return new HttpClient(handler);
Expand All @@ -61,8 +70,10 @@ public virtual HttpMessageHandler CreateInnerHandler() {
if (handler.SupportsAutomaticDecompression)
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

try { handler.UseCookies = false; }
catch (PlatformNotSupportedException) { } // look out for WASM platforms (#543)
if (!_isBrowser) {
try { handler.UseCookies = false; }
catch (PlatformNotSupportedException) { } // already checked for Blazor, but just in case any other surprises pop up
}

return handler;
}
Expand Down

0 comments on commit 1bb423a

Please sign in to comment.