Skip to content
mcNetsPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

11 Commits

Folders and files

Repository files navigation

UnoGrpc

This project should solve this issue:

unoplatform/uno#1594

Project to show how to use gRPC in an Uno Platform application.

Tested on:

[x] Desktop [x] Wasm [x] Android

The challenges of using gRPC in Uno are the following:

  • A server must be started before to execute the app.

  • Both client packages must be included because WebAssembly uses the Web version and the rest do not.

<PackageVersion Include="Grpc.Net.Client" Version="2.76.0" />
<PackageVersion Include="Grpc.Net.Client.Web" Version="2.76.0" />
  • By default the gRPC server must accept HTTP1 and HTTP2, since WebAssembly only uses HTTP1.
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http1AndHttp2"
    }
  }
}
  • If WebAssembly support is needed, CORS must be configured on the gRPC server, which requires knowing the client URLs and using GrpcWeb.
builder.Services.AddGrpc();
builder.Services.AddCors(options =>
{
    options.AddPolicy("Wasm", p => p
        .WithOrigins("http://localhost:5000", "https://localhost:5001")
        .AllowAnyHeader()
        .AllowAnyMethod()
        .AllowCredentials());
});

var app = builder.Build();

app.UseRouting();
app.UseGrpcWeb();
app.UseCors();

app.MapGrpcService<WeatherService>().EnableGrpcWeb().RequireCors("Wasm");
app.MapGet("/", () => "gRPC service");

app.Run();
  • For Android I had to add UseNativeHttpHandler to the project.
    <PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">
        <UseNativeHttpHandler>false</UseNativeHttpHandler>
    </PropertyGroup>

and configure the service depending on whether it is Android, Desktop, or WebAssembly:

    private readonly string _channelUrl =
#if __ANDROID__
        "https://10.0.2.2:7014";
#else
        "https://localhost:7014";
#endif

    private GrpcChannel CreateChannel()
    {
#if __WASM__
        var wasmHandler = new GrpcWebHandler(GrpcWebMode.GrpcWebText, new HttpClientHandler());
        return GrpcChannel.ForAddress(_channelUrl, new GrpcChannelOptions
        {
            HttpHandler = wasmHandler
        });
#elif __ANDROID__
        var androidHandler = new SocketsHttpHandler();
#if DEBUG
        androidHandler.SslOptions.RemoteCertificateValidationCallback = (_, _, _, _) => true;
#endif
        return GrpcChannel.ForAddress(_channelUrl, new GrpcChannelOptions
        {
            HttpHandler = androidHandler
        });
#else
        return GrpcChannel.ForAddress(_channelUrl);
#endif
    }

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages