Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Core gRPC Server in Dotnet Framework 4.8 client?

Tags:

c#

grpc

I've created a DotNet Core 3.1 gRPC server.

Is it possible to user this server in a DotNet Frametwork 4.8 client?

I've referenced these packages:

  • Google.Protobuf
  • Grpc.Core
  • Grpc.Core.Api

My test code looks like this:

using Grpc.Core;
using System;
using System.Windows.Forms;

namespace DotNetgRpcTest.ClientWinformC
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);

            var client = new Greeter.GreeterClient(channel);
            String user = "John";

            var reply = client.SayHello(new HelloRequest { Name = user });
            Console.WriteLine("Greeting: " + reply.Message);           
        }
    }
}

I've copied the greet.proto from the server...

enter image description here

But it can't find Greeter in the code above.

Is it at all poosible to do?

UPDATE.......

I managed to put the Protos in a .Net Standard 2.0 library with these nugets:

  • Grpc
  • Grpc.Core
  • Grpc.Tools

Next problem...

When I run the apps, my .Net Frmawork 4.8 app gives me this error:

Grpc.Core.RpcException: 'Status(StatusCode=Unavailable, Detail="failed to connect to all addresses")'

From google I learned that this code in the server would fix it...

webBuilder.ConfigureKestrel(options =>
{
    // This endpoint will use HTTP/2 and HTTPS on port 5001.
    options.Listen(IPAddress.Any, 5001, listenOptions =>
    {
        listenOptions.Protocols = HttpProtocols.Http2;
    });
});

Well it did - now my .Net Framework 4.8 app works .... but now my .Net Core client app gives me this error:

Grpc.Core.RpcException: 'Status(StatusCode=Internal, Detail="Error starting gRPC call: The SSL connection could not be established, see inner exception.")'
like image 289
MojoDK Avatar asked Apr 24 '20 11:04

MojoDK


1 Answers

Yes, it is possible to have a client written in .NET Framework 4.8 and a server in .NET Core 3.1. It's possible to have a client and a server written in different programming languages as well, so there's no reason that your use case wouldn't be possible. However, it's not possible to reference a .NET Core assembly from a .NET Framework assembly.

Since gRPC doesn't require a reference from the client assembly to the server assembly, your use case is perfectly fine.

The reason you cannot find the Greeter class in your code could be one of the following:

  1. You're missing a NuGet package which automatically generates C# code out of the proto files. Make sure you installed Grpc, Grpc.Tools and Google.Protobuf NuGet packages in your project.
  2. You forgot to add a reference to the proto file in the project file.
  <ItemGroup>
    <Protobuf Include="<directory_path>/greet.proto" />
  </ItemGroup>

Also, I would suggest you to have a separate .NET Standard project where you will keep the .proto files common to the client and the server. This way you don't need to copy around the .proto files and make sure that their contents are synchronized.

Btw, one trick to include all .proto files in your project without explicitly specifying their names is to use a regular expression:

  <ItemGroup>
    <Protobuf Include="**/*.proto" />
  </ItemGroup>
like image 148
Ivan Ivković Avatar answered Nov 14 '22 14:11

Ivan Ivković