grpc / grpc-dotnet

gRPC for .NET
Apache License 2.0
4.2k stars 769 forks source link

gRPC load balancer with HttpClientHandler for blazor seems not working #2038

Closed moezRebai closed 1 year ago

moezRebai commented 1 year ago

Hello,

I m trying to add a client side load balancing for my Blazor application using a static Resolver , but it's not working and I m getting the below exception :

The underlying HTTP transport must be a SocketsHttpHandler with no SocketsHttpHandler.ConnectCallback configured. The HTTP transport must be configured on the channel using GrpcChannelOptions.HttpHandler. System.InvalidOperationException: Channel is configured with an HTTP transport doesn't support client-side load balancing or connectivity state tracking. The underlying HTTP transport must be a SocketsHttpHandler with no SocketsHttpHandler.ConnectCallback configured. The HTTP transport must be configured on the channel using GrpcChannelOptions.HttpHandler.

I tried to use the SocketsHttpHandle, still getting a new exception

_Unhandled exception rendering component: Operation is not supported on this platform. System.PlatformNotSupportedException: Operation is not supported on this platform. at System.Net.Http.SocketsHttpHandler.get_ConnectTimeout()

Here's my code :

        var factory = new StaticResolverFactory(_ => new[]
        {
             new BalancerAddress("localhost", 5020),
             new BalancerAddress("localhost", 5021)
         });  
        services.AddSingleton<ResolverFactory>(factory);

        services.AddSingleton(_ =>
        {
            var httpHandler = new GrpcWebHandler(GrpcWebMode.GrpcWebText, new HttpClientHandler());
            // var httpHandler = new SocketsHttpHandler();

            return GrpcChannel.ForAddress("static:///localhost", new GrpcChannelOptions
            {
                HttpHandler = httpHandler,
                Credentials = ChannelCredentials.Insecure,
                DisableResolverServiceConfig = true,

                ServiceConfig = new ServiceConfig
                {
                    //MethodConfigs = { defaultMethodConfig },
                    LoadBalancingConfigs = { new RoundRobinConfig() }
                },

                ServiceProvider = services.BuildServiceProvider(),
                MaxRetryAttempts = 100,
                UnsafeUseInsecureChannelCallCredentials = true,
            });
        });

I m using ::

JamesNK commented 1 year ago

Load balancing isn't supported on Blazor. The HTTP client in the browser doesn't have enough features to support it.

moezRebai commented 1 year ago

@JamesNK thanks for the prompt answer, however in this demo, seems to be a Blazor app, https://www.youtube.com/watch?v=CXH_jEa8dUw

JamesNK commented 1 year ago

I see your confusion. Load balancing isn't supported on Blazor WebAssembly. It is supported with Blazor server. The demo used Blazor server.

moezRebai commented 1 year ago

@JamesNK got it, thank you for the answer đź‘Ť, is there any alternatives ?

JamesNK commented 1 year ago

You could try to simulate load balancing in your app. However, Blazor WebAssembly has limited network capabilities.