Is it possible to create a GRPC Console app as a Server?
Asked Answered
C

6

10

I am using .NET Core 3.1 and want to create a GRPC Service with a Console App as the Server and a WPF App as the Client.

I can't see any examples online - for some reason all the Console Apps seem to connect and send messages, none are Servers.

Is it possible to have a Console App starting the Service and a WPF App connecting and sending a message to it?

I have downloaded below project and am trying to see if I can get a Console App to be the Server. [GRPC in .NET Core][1]

Any pointers appreciated. [1]: https://www.jenx.si/2019/06/14/experimenting-with-grpc-in-net-core/

The Console App below is now the Server but I am not able to read and store the Payload from the Client in the Main function - I can see it is received in from the Client.

How can I store the payload message from the client in the Main function?

//Console App Main function listening for

static async Task Main(string[] args)
{
        _gRpcServer = new Server
        {
            Services ={Jenx.Grpc.SimpleDemo.Contracts.JenxSimpleGrpcService.BindService(new ConsoleAppGrpcClient())},
            Ports = {new ServerPort("localhost", 505050, ServerCredentials.Insecure)}
        };
}

//receives Client Message successfully
public override Task<ReplyMessage> SendMessage(RequestMessage request, ServerCallContext context)
    {
        Console.WriteLine($"Message Received: {request.RequestPayload}");
        Console.WriteLine("Sending message back to the client...");

//<<---need to Store below in variable and return to Main function above--->>
        return Task.FromResult(new ReplyMessage { ResponsePayload = $"ServerSideTime is {DateTime.Now}" });
    }
Constrained answered 29/9, 2020 at 16:1 Comment(3)
How would a console app that's just a regular console app "serve" anything? Sure, you can make them host services. But. Asp.net web api apps are effectively console apps, but with the capability to receive requests and return responses. I am therefore wondering why you'd want a console app pecifically rather than something more like learn.microsoft.com/en-us/aspnet/core/tutorials/grpc/…Prosit
We already have a Console App, now calls a WPF form (on a separate process) allowing the user to enter some data. Once the user has entered the data the program comes back to the Console App - we need the data the user has entered....Constrained
Did you figure it out?Nuzzi
V
6

I have been working from the start with console grpc server for testing purposes, first in .net 2.1 and now in 5.0.

    class Program
{
    const string ServerEndpoint = "localhost";
    const int Port = 50051;
    const string ExitCommand = "exit";

    static void Main(string[] args)
    {
        var serverStub = new MyGrpcServerStub();

        var server = new Server
        {
            Services =
            {
                grpcNameSpace.GrpcServer.BindService(serverStub),
            },
            Ports = {new ServerPort(ServerEndpoint, Port, ServerCredentials.Insecure)}
        };
        server.Start();

        var keyStroke = string.Empty;
        while (keyStroke != ExitCommand)
        {
            var random = new Random();
            keyStroke = Console.ReadLine();

            if (!string.IsNullOrWhiteSpace(keyStroke) && keyStroke != ExitCommand)
            {
                // Do some stuff to trigger things on the server side

            }
        }

        serverStub.CloseStreams();
        server.ShutdownAsync().Wait();
    }
}

Just inherit from the Grpc server base created by the Protobuf compiler, and overide all RPC calls from the proto like this:

    public class MyGrpcServerStub : grpcNameSpace.GrpcServer.MyXXXXBase
{

    public override Task<BoolValue> GetStuff(ProtoVersion request, ServerCallContext context)
    {
        return Task.FromResult(new BoolValue() { Value = true});
    }
}

For testing purposes I'm storing values inside the MyGrpcServerStub, but you can store it where ever you like.

Verlie answered 30/11, 2020 at 15:22 Comment(0)
P
3

You should look at the Helloworld example in the gRPC GitHub repository. It contains a simple gRPC service where both the server part and the client part are C# console applications. Uses .NET SDK Core 2.1, but should work in .NET Core 3.1+ and .NET 5+ (I didn't try it on those versions).

Phonotypy answered 16/9, 2021 at 19:0 Comment(2)
The Server part uses Grpc.Core which is not recommend/maintained anymore - grpc.io/blog/grpc-csharp-futureAnatolia
This example has been removed and no longer exists.Lutz
D
2

In .NET Core 3.1 a "gRPC Service" project is just a Console App with extra stuff added in the project template. A Console window shows up when you run it and you can write text to the console (usually through a Logger). If for some reason you can't use a new gRPC Service project you can still create a temporary one and cut and paste the extra files (Program.cs, Startup.cs, etc.) and Dependencies into your Console app.

Devoid answered 1/10, 2020 at 3:24 Comment(1)
I want to be able to store the received message from the Client into the Console App - I just don't know how to do that...Constrained
C
0

To store the message received from the client, you can add a received notification handler and within the handler, you could add your process for storing the received messages. Utilizing the MediatR framework

Conception answered 5/9, 2021 at 14:52 Comment(1)
Please add further details to expand on your answer, such as working code or documentation citations.Fowlkes
L
0

https://github.com/grpc/grpc-dotnet is the recommended/maintained usage for gRPC+c#, unfortunately all the examples seem to use ASP.NET Core

"There are too many pending edits on Stack Overflow. Please try again later." on @Rui MGS Bras answer and I don't have the reputation to comment so I have to submit this as a new answer

The csharp example was removed from the gRPC repository but can be found here: https://github.com/grpc/grpc/tree/v1.46.x/examples/csharp

note:

Starting from May 2021, gRPC for .NET is the recommended implemention of gRPC for C#. The original gRPC for C# implementation (distributed as the Grpc.Core nuget package) is now in maintenance mode and will be deprecated in the future. See blogpost for more details.

This directory used to contain the original C# implementation of gRPC based on the native gRPC Core library (i.e. Grpc.Core nuget package). This implementation is currently in maintenance mode and its source code has been moved (see below for details). Also, we plan to deprecate the implementation in the future (see blogpost).

That version "contains a simple gRPC service where both the server part and the client part are C# console applications."

Leningrad answered 26/6 at 17:34 Comment(0)
C
-1

I hope you are well. About your ask, you can create a Console programm and use them to host a WCF Service to publish access to Client WPF or WinForm Client. In this architecture, you have 3 layers. The Database Layer, the middle Business Layer and Client Layer and the connection can be over TCP, or HTTPS, as you configure the End Points. Also it's possible host the WCF in a Windows Service or Daemon. In .NET Core I think you can create WCF service or similar and host them in a Console Program or Service.

Cybill answered 29/9, 2020 at 16:27 Comment(2)
We want to use GRPCConstrained
I know you want to use GRPC, I only want to give you a directionCybill

© 2022 - 2024 — McMap. All rights reserved.