Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The ConfigureHttpClient method is not supported when creating gRPC clients. #4924

Open
eerhardt opened this issue Feb 6, 2024 · 7 comments
Assignees
Labels
area-resilience blocked bug This issue describes a behavior which is not expected - a bug.

Comments

@eerhardt
Copy link
Member

eerhardt commented Feb 6, 2024

Description

3654748 is breaking using gRPC clients with Http.Resilience. This is a breaking change from 8.1.

Reproduction Steps

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.2.0-preview.24105.1" />

    <PackageReference Include="Google.Protobuf" Version="3.25.2" />
    <PackageReference Include="Grpc.Net.ClientFactory" Version="2.59.0" />
    <PackageReference Include="Grpc.Tools" Version="2.59.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  </ItemGroup>
  
</Project>
using GrpcGreeter;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddGrpcClient<Greeter.GreeterClient>(o =>
{
    o.Address = new Uri("https://localhost:5001");
});

builder.Services.ConfigureHttpClientDefaults(http =>
{
    // Turn on resilience by default
    http.AddStandardResilienceHandler();
});

var app = builder.Build();

// Configure the HTTP request pipeline.

var summaries = new[]
{
    "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/hello", (Greeter.GreeterClient client) =>
{
    return "hello";
});

app.Run();

greet.proto:

syntax = "proto3";

option csharp_namespace = "GrpcGreeter";

package greet;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply);
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings.
message HelloReply {
  string message = 1;
}

Expected behavior

The app should work, like it did when I use <PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.2.0-preview.24105.1" />

Actual behavior

The app fails with:

An unhandled exception occurred while processing the request.
InvalidOperationException: The ConfigureHttpClient method is not supported when creating gRPC clients. Unable to create client with name 'GreeterClient'.
Grpc.Net.ClientFactory.Internal.GrpcCallInvokerFactory.CreateInvoker(EntryKey key)

Stack Query Cookies Headers Routing
InvalidOperationException: The ConfigureHttpClient method is not supported when creating gRPC clients. Unable to create client with name 'GreeterClient'.
Grpc.Net.ClientFactory.Internal.GrpcCallInvokerFactory.CreateInvoker(EntryKey key)
System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>.GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
Grpc.Net.ClientFactory.Internal.GrpcCallInvokerFactory.CreateInvoker(string name, Type type)
Grpc.Net.ClientFactory.Internal.DefaultGrpcClientFactory.CreateClient<TClient>(string name)
Microsoft.Extensions.DependencyInjection.GrpcClientServiceExtensions+<>c__DisplayClass7_0<TClient>.<AddGrpcHttpClient>b__2(IServiceProvider s)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitDisposeCache(ServiceCallSite transientCallSite, RuntimeResolverContext context)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSite(ServiceCallSite callSite, TArgument argument)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine+<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
lambda_method2(Closure , object , HttpContext )
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

Regression?

Yes, from 8.1

Known Workarounds

Don't call AddStandardResilienceHandler() on HttpClients that use gRPC.

Configuration

net8.0

Other information

cc @martintmk @geeknoid @joperezr @JamesNK

@eerhardt eerhardt added untriaged bug This issue describes a behavior which is not expected - a bug. labels Feb 6, 2024
@joperezr
Copy link
Member

joperezr commented Feb 6, 2024

@martintmk can you please take a look at this? As specified above, this is a regression and breaking change, and we are preparing to lock for 8.2 release, so time is running out for fixing this. If fixing this is not trivial, I'd suggest reverting the change that introduced the regression for 8.2, and then we can check on how to better fix the original issue without causing a regression for 8.3. Thoughts?

eerhardt added a commit to dotnet/aspire that referenced this issue Feb 7, 2024
* Revert "Update Microsoft.Extensions.Http.Resilience to 8.2 (#2094)"

This reverts commit 2bf302b.

There is a breaking change with Grpc clients in this version. See dotnet/extensions#4924

* Set the EndToEnd test HttpClient timeout to infinite

This allows the Http.Resilience handlers to handle the timeout correctly.
@martintmk
Copy link
Contributor

@joperezr Let's just revert that fix for 8.2.0, service owners can still disable the timeout as a workaround.

Unless the restrictions around using ConfigureHttpClient is lifted for GRPC clients, I don't see any other way to fix this. (other than manually disabling timeout for HttpClient.

I must say the behavior of ConfigureHttpClient is strange, would love know the reason why it is not allowed.

@eerhardt
Copy link
Member Author

eerhardt commented Feb 7, 2024

I must say the behavior of ConfigureHttpClient is strange, would love know the reason why it is not allowed.

+1.

@JamesNK, can you clue us in on why this is restricted?

radical pushed a commit to radical/aspire that referenced this issue Feb 7, 2024
* Revert "Update Microsoft.Extensions.Http.Resilience to 8.2 (dotnet#2094)"

This reverts commit 2bf302b.

There is a breaking change with Grpc clients in this version. See dotnet/extensions#4924

* Set the EndToEnd test HttpClient timeout to infinite

This allows the Http.Resilience handlers to handle the timeout correctly.
@JamesNK
Copy link
Member

JamesNK commented Feb 7, 2024

I must say the behavior of ConfigureHttpClient is strange, would love know the reason why it is not allowed.

+1.

@JamesNK, can you clue us in on why this is restricted?

Because it has no impact. It's to let the developer know that this configuration they added to the gRPC client won't do anything.

builder.Services.AddGrpcClient<Greeter.GreeterClient>(o =>
{
    o.Address = new Uri("https://localhost:5001/");
})
.ConfigureHttpClient(options => options.Timeout = TimeSpan.FromSeconds(10)); // no impact

However, now that HttpClientFactory has the concept of applying defaults across all clients it isn't as useful. Changes would be to either add an option to gRPC client factory to suppress the error or changing the error to logging.

@martintmk
Copy link
Contributor

@JamesNK

Changes would be to either add an option to gRPC client factory to suppress the error or changing the error to logging.

My vote would be to apply this change and once the new version of the library (gRPC ??) is in we can revert #4925 again and re-introduce the fix.

@eerhardt Thoughts?

@iliar-turdushev
Copy link
Contributor

However, now that HttpClientFactory has the concept of applying defaults across all clients it isn't as useful. Changes would be to either add an option to gRPC client factory to suppress the error or changing the error to logging.

@JamesNK Are there any updates to the suggestion above (suppress the error or log it)? If not, is there a ticket/item to tract it? Thank you.

@JamesNK
Copy link
Member

JamesNK commented Apr 30, 2024

There aren't any updates. I've created an issue to track improving gRPC - grpc/grpc-dotnet#2425

I'm really busy so I'm not sure when I'll get time to look at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-resilience blocked bug This issue describes a behavior which is not expected - a bug.
Projects
None yet
Development

No branches or pull requests

7 participants