unoplatform / uno

Open-source platform for building cross-platform native Mobile, Web, Desktop and Embedded apps quickly. Create rich, C#/XAML, single-codebase apps from any IDE. Hot Reload included! 90m+ NuGet Downloads!!
https://platform.uno
Apache License 2.0
8.99k stars 730 forks source link

Examples how to use Uno with gRPC #1594

Open ghuntley opened 5 years ago

ghuntley commented 5 years ago

What would you like to be added:

Instructions how to use grpc w/Uno (specifically WebAssembly)

Why is this needed:

Pre-emptive request as there were announcements recently at .NET conf about grpc.

For which Platform:

Anything else we need to know?

jhalak27 commented 4 years ago

Hey, I am new here. Could you let me know what needs to de done here?

jeromelaban commented 4 years ago

Thanks for taking a stab at it @jhalak27!

A first step would be to try using the commonly known grpc.net samples to see what works, what does not, and post your findings here.

MartinZikmund commented 3 years ago

@jhalak27 are you still interested in this one?

inforzip commented 3 years ago

I'm using a Uno, but I'm having problems using GRPC. I have installed the Grpc.Net.Client , Google.Protobuf and Grpc.Tools packages on all platforms, but i can't create GrpcChannel or use grpc services. It is possible to use GRPC on Uno project or must chose onother RPC (WCF, REST ...)

jeromelaban commented 3 years ago

@inforzip we do not have documentation to use this, as you've seen. If you can provide a repro sample to this issue of what you're trying to do, it'll help.

inforzip commented 3 years ago

I apologize for late reply. We append a test project with the problem that is happening to us with grpc client. We managed to establish the channel with the grpc server side, but when we need to call the class new Greeter.GreeterClient(channel) is not present, we think because the grpc protobuf class generator is not called and does not create the respective classes from the .proto files located in shared project. Any help is well comming xizSoft.zip .

MarkStega commented 3 years ago

I don't have the Uno tools installed, but in a csproj the following gives the required properties to compile protobufs

<ItemGroup>
        <Protobuf Include="gRPCClient\codeProto.proto" />
        <Protobuf Include="gRPCClient\persistentStorageProxyProto.proto" />
        <Protobuf Include="gRPCClient\documentProto.proto" />
        <Protobuf Include="gRPCClient\practiceProto.proto" />
        <Protobuf Include="gRPCClient\equipmentProto.proto" />
        <Protobuf Include="gRPCClient\providerProto.proto" />
        <Protobuf Include="gRPCClient\reportDefinitionProto.proto" />
        <Protobuf Include="gRPCClient\reportProto.proto" />
        <Protobuf Include="gRPCClient\userProto.proto" />
    </ItemGroup>

Protobuf properties

and the dll getting created has the following nuget dependencies installed:

devenv_2021-11-17_12-53-36

inforzip commented 2 years ago

I appreciate all the help given so far.

I have been testing all the suggestions that have come to me, but the problem has not been solved. This leads to frustration due to the time spent and not seeing any light at the end of the tunnel. I have all the nuget packages installed (grpc.core;grpc.net.client;grpc.tools;google.protobuf). I manage to create the connection with the grpc server, but when I try to create the client I get the error: The CompileXaml task returned false but did not log an error. testGrpc2.Uwp although the classes are created in the \obj folder by google.protobuf.

var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions
{
    HttpHandler = new System.Net.Http.HttpClientHandler(),
});

//Comment bottom line to cleaar the issue
var client = new Greet.Greeter.GreeterClient(channel);

What will be causing the crap of this error. I have already eliminated the \obj and \bin folders and restarted visual studio but the problem remains. Any clue is welcome.

MarkStega commented 2 years ago

@inforzip How about either posting a new zip file or creating a public github repository of your test project?

inforzip commented 2 years ago

I've tried everything, please help

Download the project from this link https://inforzip.com/testGrpc2.zip

inforzip commented 2 years ago

I already deleted the \obj folder and recompile the project but the problem remains. This situation happens in the uno project that I made available but also in a uwp project. In console and wpf projects everything seems to work much better I've already tested it with visual studio 2019 and 2022 and 2022 prev 1.1 with .net5 and .net6 and the result is always the same

jeromelaban commented 2 years ago

@inforzip Thanks for the repro. You'll need to make sure that the proto file is accessible from all "Head" projects (it's only in the UWP project in your sample).

The easiest way is to create a netstandard2.0 library project, place the proto files there, then reference that project in all project heads.

inforzip commented 2 years ago

First of all I want to thank you for all the help provided.

With the help of jerome i managed to move forward. Now i can create the channel, create the client, make the request to the grpc server. Everything seemed perfect until i verified that the server receives the client's request, processes it and responds., but the client receives the following error

StatusCode="Cancelled", Detail="No grpc-status found on response".

This is my code:

AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http1UnencryptedSupport", true);
var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
// The error happens on the next line
var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions
  {
      HttpHandler = handler,
      Credentials = Grpc.Core.ChannelCredentials.SecureSsl
  });
var client = new Greeter.GreeterClient(channel);

Thanks in advance

mcNets commented 1 year ago

Hi I am currently using gRPC in a UNO project (Windows + Skia), I can test it with Android emulators but not with IOS, my Apple developer license expired long ago.

mcNets commented 1 year ago

Android head issues:

The issue I found with the Android head is related to grpc_csharp_ext library. It seems to be a Windows-only library so it has to be somehow compiled specifically for Android.

Loaded assembly: /data/data/McNets.UnoGrpc/files/.__override__/System.Text.RegularExpressions.dll [External]
[monodroid-assembly] Shared library 'grpc_csharp_ext' not loaded, p/invoke 'grpcsharp_redirect_log' may fail
[monodroid-assembly] Shared library 'grpc_csharp_ext' not loaded, p/invoke 'grpcsharp_redirect_log' may fail
**System.InvalidOperationException:** 'Failed to load UnoGrpc.MainPage: System.DllNotFoundException: grpc_csharp_ext

I'am trying to find a workaround in grpc and Xamarin repos but I haven't find out yet. This libary seems to belog to Grpc.Core that is not maintained currently.

You can read about it;

https://github.com/grpc/grpc-dotnet/issues/2126 https://github.com/grpc/grpc/issues/16250 https://github.com/grpc/grpc/issues/16310

This is my repo:

https://github.com/mcNets/UnoGrpc

NOTE: Start the server in a separated terminal.

mcNets commented 1 year ago

Ok, I take this.

Should I post a blog article?

I'll try to add a new article and a new sample following: Contributing docs to Uno Platform