2

I want to create a gRPC service but I need to host in a winform .net application. there is an extraordinary example of how Hosting ASP.NET Core API in a Windows Forms Application I would like someone to explain to me, what I need to install and what I should change in that example, to host a grpc service in the form of Windows ...

  • Well, you would do exactly same as in that post, but just use grpc endpoints (endpoints.MapGrpcService) like here - https://learn.microsoft.com/en-us/aspnet/core/grpc/?view=aspnetcore-3.1 instead of mvc endpoints . So again - same as in that post, but just replace mvc setup with grpc setup as in official documentation – Nikita Chayka Nov 01 '20 at 11:50
  • 1
    The link is creating a client and a server and connecting the client to the server. The window form is the client and the controller is the server. A windows form project normally has a Startup.cs with a call to the form which has a [STAThread]. The link is starting the WebHost before the form starts so the server starts before the client. The normal form code is in the Startup class of the form. So when you create a new form project add the code in step 7 to the program.cs. Then step 6 goes in the main form module. – jdweng Nov 01 '20 at 11:52
  • Thanks for answering. I have tried to do following the tutorial but I can't do it because gRPC generates the service automatically and saves it in the obj folder, and I can't generate it, the other problem is that services.AddGrpc () uses an extension of the Grpc.AspNetCore.Server library which is not compatible with .net framework 4.X. so i see that it is not supported apparently – Fabian Wesling Nov 01 '20 at 12:30

1 Answers1

2

You could follow the same steps but with several additional:

  1. Install the package Microsoft.AspNetCore.Grpc.HttpApi This is going to map your gRPC endpoints to the classical HTTP. It is not automatic you need to specify the services in the Startup.cs as follow:
    app.UseEndpoints(endpoints =>
            {
                endpoints.MapGrpcService<MygRPCService>();
            });
  1. Into your protos you need to indicate the HTTP path, something like this:
     rpc Get(GetRequest) returns (GetReply) {
        option (google.api.http) = {
          get: '/my-endpoint'
          body: '*'
        };
      }
  1. Add to your Startup.cs ConfigureService method:
    services.AddGrpc();
    services.AddGrpcHttpApi(); 
Jesus Santander
  • 315
  • 3
  • 10