Use Core gRPC Server in Dotnet Framework 4.8 client?
Asked Answered
P

2

10

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.")'
Previse answered 24/4, 2020 at 11:9 Comment(0)
F
9

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>
Frustrated answered 29/4, 2020 at 8:38 Comment(2)
Well, I would need to see the code to check what's wrong, but it seems to me that the solution you googled is unnecessary. gRPC is straightforward and easy to use and I've never used something like that. Did you forget to start your server maybe? Check out how to do that here: grpc.io/docs/tutorials/basic/csharp/#starting-the-serverPigeonhole
Does anyone know an answer to this problem?: #65057950Wheeling
B
0

You need to configure the channel to use WinHttpHandler. And add System.Net.Http.WinHttpHandler nuget package https://learn.microsoft.com/en-us/aspnet/core/grpc/netstandard?view=aspnetcore-8.0#net-framework

var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions
{
    HttpHandler = new WinHttpHandler()
});
Beffrey answered 20/7 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.