Cysharp / GrpcWebSocketBridge

Yet Another gRPC over HTTP/1 using WebSocket implementation, primarily targets .NET platform.
MIT License
77 stars 12 forks source link

Channel is not available in Android MAUI application #32

Open mayuki opened 5 months ago

mayuki commented 5 months ago

Original Issue #26


Here is my test results.

Environment: Windows 11. (All nugets latest, magiconion and grpcwebsocketbridge. Application: Net MAUI Blazor Hybrid

Connection from Windows: Successful with new upgrade.

Case 1: Connecting from Windows MAUI App to MagicOnionGrpcServer

Channel Configuration code:

GrpcChannel channel?;
 if (OperatingSystem.IsWindows())
 {
     var httpHandler = new GrpcWebSocketBridgeHandler();  **//Note there was no need to forceWebSocketMode, though it works with that as sell**
     channel = GrpcChannel.ForAddress(baseUri, new GrpcChannelOptions
     {
         HttpHandler = httpHandler,
         MaxReceiveMessageSize = 20 * 1024 * 1024, // 10 MB
         MaxSendMessageSize = 10 * 1024 * 1024, // 10 MB
     });
 }

Results: Working as expected.

Case 2: Connecting from Android MAUI App to MagicOnionGrpcServer (Same App but running Android Build)

if (OperatingSystem.IsAndroid())
{
    var httpHandler = new GrpcWebSocketBridgeHandler(true); //Does not work with any either true or false for forceWebSocket
    channel = GrpcChannel.ForAddress(baseUri, new GrpcChannelOptions
    {
        HttpHandler = httpHandler,
        MaxReceiveMessageSize = 20 * 1024 * 1024, // 10 MB
        MaxSendMessageSize = 10 * 1024 * 1024, // 10 MB
    });
}
[chromium] [INFO:CONSOLE(1)] "The channel configuration isn't valid on Android devices. The channel is configured to use HttpClientHandler and Android's native HTTP/2 library. gRPC isn't fully supported by Android's native HTTP/2 library and it can cause runtime errors. To fix this problem, either configure the channel to use SocketsHttpHandler, or add <UseNativeHttpHandler>false</UseNativeHttpHandler> to the app's project file. For more information, see https://aka.ms/aspnet/grpc/android.
[chromium]    at Grpc.Net.Client.GrpcChannel.CreateInternalHttpInvoker(HttpMessageHandler handler) in /_/src/Grpc.Net.Client/GrpcChannel.cs:line 501
[chromium]    at Grpc.Net.Client.GrpcChannel..ctor(Uri address, GrpcChannelOptions channelOptions) in /_/src/Grpc.Net.Client/GrpcChannel.cs:line 161
[chromium]    at Grpc.Net.Client.GrpcChannel.ForAddress(Uri address, GrpcChannelOptions channelOptions) in /_/src/Grpc.Net.Client/GrpcChannel.cs:line 697
[chromium]    at Grpc.Net.Client.GrpcChannel.ForAddress(String address, GrpcChannelOptions channelOptions) in /_/src/Grpc.Net.Client/GrpcChannel.cs:line 667
[chromium]    at Insights.Services.Client.NativeGrpcClientChannel.GetGrpcChannel(String baseUri) in D:\Trade\InsightsOnDaprAspire\src\Services\Client\NativeGrpcClientChannel.cs:line 45
[chromium]    at Insights.Core.Client.Grpc.GrpcClients.EquitiesGrpcServiceClient.GetClient() in D:\Trade\InsightsOnDaprAspire\src\Core\Client\Grpc\GrpcClients\EquitiesGrpcServiceClient.cs:line 68
[chromium]    at Insights.Core.Client.Grpc.GrpcClients.EquitiesGrpcServiceClient..ctor(IGrpcClient nativeGrpcClient, IConfiguration configuration, ILogger`1 logger, IBlazorUserManager blazorUserManager) in D:\Trade\InsightsOnDaprAspire\src\Core\Client\Grpc\GrpcClients\EquitiesGrpcServiceClient.cs:line 31
[chromium]    at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Constructor(Object obj, IntPtr* args)
[chromium]    at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
[chromium]    at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[chromium]    at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite callSite, RuntimeResolverContext argument)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite callSite, RuntimeResolverContext argument)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite callSite, RuntimeResolverContext argument)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite callSite, RuntimeResolverContext argument)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite callSite, RuntimeResolverContext argument)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite callSite, RuntimeResolverContext argument)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(ServiceIdentifier serviceIdentifier)
[chromium]    at System.Collections.Concurrent.ConcurrentDictionary`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceIdentifier, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.Extensions.DependencyInjection.ServiceProvider.ServiceAccessor, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].GetOrAdd(ServiceIdentifier key, Func`2 valueFactory)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
[chromium]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
[chromium]    at Microsoft.AspNetCore.Components.ComponentFactory.<>c__DisplayClass9_0.<CreatePropertyInjector>g__Initialize|1(IServiceProvider serviceProvider, IComponent component)
[chromium]    at Microsoft.AspNetCore.Components.ComponentFactory.InstantiateComponent(IServiceProvider serviceProvider, Type componentType, IComponentRenderMode callerSpecifiedRenderMode, Nullable`1 parentComponentId)
[chromium]    at Microsoft.AspNetCore.Components.RenderTree.Renderer.InstantiateChildComponentOnFrame(RenderTreeFrame[] frames, Int32 frameIndex, Int32 parentComponentId)
[chromium]    at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewComponentFrame(DiffContext& diffContext, Int32 frameIndex)
[chromium]    at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewSubtree(DiffContext& diffContext, Int32 frameIndex)
[chromium]    at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InsertNewFrame(DiffContext& diffContext, Int32 newFrameIndex)
[chromium]    at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext& diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl)
[chromium]    at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.ComputeDiff(Renderer renderer, RenderBatchBuilder batchBuilder, Int32 componentId, ArrayRange`1 oldTree, ArrayRange`1 newTree)
[chromium]    at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
[chromium]    at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch(RenderQueueEntry renderQueueEntry)
[chromium]    at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()", source: https://0.0.0.0/_framework/blazor.webview.js (1)
[GPUAUX] [AUX]GuiExtAuxCheckAuxPath:663: Null anb

Result: Does not work on MAUI Android App. with above configuration.

Workaround/Perhaps right way to create channel specific to MAUI

After a little research and guidance from a reply of @JamesNK, I figured out that correct way to create a channel on MAUI app should be.

if (OperatingSystem.IsAndroid())
{
    var httpHandler = new GrpcWebSocketBridgeHandler(true); //Works with both true/false
    channel = GrpcChannel.ForAddress(baseUri, new GrpcChannelOptions
    {
        HttpClient = new HttpClient(httpHandler),   //Observe that we are passing HttpClient with GrpcWebSocketBridgeHandler
        MaxReceiveMessageSize = 20 * 1024 * 1024, // 10 MB
        MaxSendMessageSize = 10 * 1024 * 1024, // 10 MB
    });
}

I belive current build 1.3 has fixed the existing issues and the issue specific to MAUI relates to the way we need to provision the channel is specific to Android.

Thanks for such great tools and libraries.

Love from New Delhi!

Originally posted by @Sen-Gupta in https://github.com/Cysharp/GrpcWebSocketBridge/issues/26#issuecomment-2049924182

mayuki commented 5 months ago

I think this may require setting <UseNativeHttpHandler>false</UseNativeHttpHandler> as written in the exception message.