Skip to content

Commit

Permalink
fix: Update gRPC adapter detection to always use Grpc.Core on .NET Fr…
Browse files Browse the repository at this point in the history
…amework

Currently, users who have a recent (extra) Grpc.Net.Client
dependency and are using .NET Framework will default to
Grpc.Net.Client, even if it won't work due to client/bidi-streaming calls.

Eventually we'll detect this properly, but for now we can just use
Grpc.Core unconditionally on .NET Framework. (This can still be
overridden with the environment variable or explicit code, of course.)

This is part of #712, but doesn't fully address it.
  • Loading branch information
jskeet committed Nov 7, 2023
1 parent 6449aa5 commit 781270b
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions Google.Api.Gax.Grpc/GrpcAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,42 @@ internal static GrpcAdapter GetDefaultFromEnvironmentVariable(string environment
};
}

// TODO: Is this really what we want? Definitely simple, but not great in other ways...
private static GrpcAdapter DetectDefaultGrpcTransportAdapterPreferringGrpcNetClient()
{
// Test 1: does Grpc.Net.Client work at all? If not, use Grpc.Core.
try
{
GrpcChannel.ForAddress("https://ignored.com");
return GrpcNetClientAdapter.Default;
}
catch
{
return GrpcCoreAdapter.Instance;
}
}
// Test 2, only for .NET Framework: check what version of Windows we're using.
#if NET462_OR_GREATER
if (!PlatformFullySupportsGrpc())
{
return GrpcCoreAdapter.Instance;
}
#endif
return GrpcNetClientAdapter.Default;


#if NET462_OR_GREATER
// gRPC support on .NET Framework is complex.
// - Some Windows platforms don't support it at all, in which case an exception is thrown
// in GrpcChannel.ForAddress, which is handled above.
// - Other platforms support it partially - unary and server-streaming, but not
// bidi- or client-streaming.
// - Other platforms support it fully.
//
// While in theory we could tailor the default instance for a given service to whether
// streaming is required or not, that would lead to a very inconsistent experience.
// At some point, we wish to check the Windows version details and use that to determine
// which adapter to use, but for now we'll always use Grpc.Core.
// Once https://github.com/grpc/grpc-dotnet/issues/2242 is fixed, we can use that code.
static bool PlatformFullySupportsGrpc() => false;
#endif
}
}
}

0 comments on commit 781270b

Please sign in to comment.