protobuf-net / protobuf-net.Grpc

GRPC bindings for protobuf-net and grpc-dotnet
Other
856 stars 109 forks source link

Is there a known issue with Xamarin? #112

Open victoriorinescu opened 4 years ago

victoriorinescu commented 4 years ago

The same code that works on windows does not work with Android (Xamarin)

`

        GrpcClientFactory.AllowUnencryptedHttp2 = true;
        var handler = new HttpClientHandler();
        var httpClient = new HttpClient(handler);
        handler.Credentials = new NetworkCredential("admin", "pass");
        var channel = GrpcChannel.ForAddress(url, new GrpcChannelOptions
        {
            HttpClient = httpClient,
        });
        var service = channel.CreateGrpcService<IMyService>();
        var people = service.GetPeople();

`

The same thing happens if I use a secured connection.

On service = channel.CreateGrpcService<IMyService>(); I get:

Grpc.Core.RpcException: 'Status(StatusCode=Internal, Detail="Error starting gRPC call. HttpRequestException: An error occurred while sending the request. IOException: The server returned an invalid or unrecognized response.")'

mgravell commented 4 years ago

are you sure that the error doesn't happen at GetPeople()? I ask because I'm not sure it has actually started talking gRPC before then, which means it shouldn't be throwing an RpcException. Fundamentally, there's two completely different things that could be at play here:

  1. a problem with the ref-emit that happens in CreateGrpcService<T>, or
  2. a problem in the underlying gRPC transport at service.GetPeople()

If the problem is "1", then it is probably a problem for me to fix; if the problem is "2", then it is probably a problem for JamesNK over in grpc/grpc-dotnet. So let's see if we can narrow it down! We can avoid "1" completely by using the non-emit API (please verify that this works on Windows before trying it on Xamarin):

var nonGeneric = channel.CreateGrpcService(typeof(IMyService));
var method = typeof(IMyService).GetMethod(nameof(IMyService.GetPeople));
#pragma warning disable CS0618 // hack around Empty marked [Obsolete]
var people = nonGeneric.BlockingUnary<Empty, People>(Empty.Instance, method);
#pragma warning restore CS0618

(where People is whatever GetPeople() returns; I can't see that in the example)

Let me know!

mgravell commented 4 years ago

also: watch out for any HTTP inspection tools that might be running, such as Fiddler. They totally break gRPC.

mgravell commented 4 years ago

doing some reading, it sounds like Xamarin gRPC support is a bit... wobbly right now, and that it is being targeted for .NET 5 timescales (later this year)

victoriorinescu commented 4 years ago

It was on people = service.GetPeople(); the debuger was shown the wrong line.

I have tried your code and also the error is on call: var people = nonGeneric.BlockingUnary<Empty, IEnumerable<Person>>(Empty.Instance, method);

on xamarin: Grpc.Core.RpcException: 'Status(StatusCode=Internal, Detail="Error starting gRPC call. HttpRequestException: An error occurred while sending the request. IOException: The server returned an invalid or unrecognized response.")'

Edit:

on windows: Actually on windows is working. I put the wrong return type (Person instead of IEnumerable<Person>).

mgravell commented 4 years ago

k, the first thing to do is (rummages for antipodean summoning kit...) @JamesNK - any idea what the state of gRPC and Xamarin is?

JamesNK commented 4 years ago

I asked the Xamarin team a number of times whether they support HTTP/2 and they never confirmed it did.

If you want to use gRPC with a Xamarin client I recommend using gRPC-Web.

victoriorinescu commented 4 years ago

Thanks.

I ended up using asp.net core with controllers.

On Wed, Jul 8, 2020 at 11:15 PM James Newton-King notifications@github.com wrote:

I asked the Xamarin team a number of times whether they support HTTP/2 and they never confirmed it did.

If you want to use gRPC with a Xamarin client I recommend using gRPC-Web.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/protobuf-net/protobuf-net.Grpc/issues/112#issuecomment-655732905, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQBN2HJCWDKLZ5HUVAL7KJLR2THVZANCNFSM4OULURMQ .

valentasm1 commented 4 years ago

Is there any way use Xamarin.Forms gRPC-Web with code first proto buf?

mgravell commented 4 years ago

Does it work with "not code first" (meaning: contract first)? If so: it should work fine with code first. If it doesn't work with "contract first", then the transport itself is probably broken, and I can't fix that. If the transport works: "code first" should probably work.

valentasm1 commented 4 years ago

My bad. I used http2 port. Even i could use http2 with Xamarin using original gRPC C# implementation, but joy of using code first stops it. Waiting for MAUI

Mrsevic commented 3 years ago

Same problem here with Grpc Web.