Arshia001 / FSharp.GrpcCodeGenerator

A protoc plugin to enable generation of F# code + supporting libraries
MIT License
81 stars 9 forks source link

Sample ASP.NET Core App? #1

Closed panesofglass closed 3 years ago

panesofglass commented 3 years ago

I'm trying to create a small sample. However, I'm not able to get the server app to compile with:

/home/ryan/.nuget/packages/grpc-fsharp.tools/0.1.0/build/_protobuf/Google.Protobuf.Tools.targets(62,5): error MSB4062: The "Grpc.Tools.ProtoToolsPlatform" task could not be loaded from the assembly /home/ryan/.nuget/packages/grpc.tools/2.34.0/build/_protobuf/netstandard2.0/Grpc-FSharp.Tools.dll. Could not load file or assembly '/home/ryan/.nuget/packages/grpc.tools/2.34.0/build/_protobuf/netstandard2.0/Grpc-FSharp.Tools.dll'. The system cannot find the file specified. [/home/ryan/Code/fsgrpc/GrpcSample/GrpcSample.fsproj]
/home/ryan/.nuget/packages/grpc-fsharp.tools/0.1.0/build/_protobuf/Google.Protobuf.Tools.targets(62,5): error MSB4062:  Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [/home/ryan/Code/fsgrpc/GrpcSample/GrpcSample.fsproj]

The client app fails to compile with:

/home/ryan/Code/grpc-fsharp-client-sample/GrpcClientSample/Program.fs(11,9): error FS0039: The value, namespace, type or module 'Greet' is not defined. [/home/ryan/Code/grpc-fsharp-client-sample/GrpcClientSample/GrpcClientSample.fsproj]
/home/ryan/Code/grpc-fsharp-client-sample/GrpcClientSample/Program.fs(14,11): error FS0039: The value, namespace, type or module 'Greet' is not defined. [/home/ryan/Code/grpc-fsharp-client-sample/GrpcClientSample/GrpcClientSample.fsproj]
/home/ryan/Code/grpc-fsharp-client-sample/GrpcClientSample/Program.fs(15,15): error FS0039: The record label 'Name' is not defined. [/home/ryan/Code/grpc-fsharp-client-sample/GrpcClientSample/GrpcClientSample.fsproj]
/home/ryan/Code/grpc-fsharp-client-sample/GrpcClientSample/Program.fs(18,9): error FS0072: Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved. [/home/ryan/Code/grpc-fsharp-client-sample/GrpcClientSample/GrpcClientSample.fsproj]
/home/ryan/Code/grpc-fsharp-client-sample/GrpcClientSample/Program.fs(20,18): error FS0072: Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved. [/home/ryan/Code/grpc-fsharp-client-sample/GrpcClientSample/GrpcClientSample.fsproj]

NOTE: I'm compiling on the CLI.

Do you happen to have a small sample to show how to correctly build the app?

Here are my initial attempts:

Can you point out what I've missed?

panesofglass commented 3 years ago

@purkhusid do you have a working sample that might help me?

purkhusid commented 3 years ago

@panesofglass I got your server working. It was just a matter of wrong namespace/types being used. In the greet.proto you have option csharp_namespace = "GrpcSample"; which makes it so that all the generated types are under the GrpcSample namespace.

I saw that you can actually see the generated .fs files under obj/Debug/net5.0/Protos/Greet.fs

If you want editor support you could also skip the MSBuild integration and create a helper that generates the files and then have a separate .fsproj for it.

It would just be a matter of:

  1. Installing protoc (https://grpc.io/docs/protoc-installation/#install-pre-compiled-binaries-any-os)
  2. Install the .Net tool: dotnet tool install -g grpc-fsharp
  3. Generate the protos with protoc protoc -I ./ ./Protos/greet.proto --fsharp_out=./output_folder

But here is the patch with the changes I needed to do with the server project. I'm pretty sure that the same applies for the client project:

diff --git a/GrpcSample/Services/GreeterService.fs b/GrpcSample/Services/GreeterService.fs
index 51db870..c8b5897 100644
--- a/GrpcSample/Services/GreeterService.fs
+++ b/GrpcSample/Services/GreeterService.fs
@@ -3,11 +3,11 @@ namespace GrpcSample
 open System

 type GreeterService() =
-    inherit Greet.GreeterService.GreeterServiceBase()
+    inherit GrpcSample.Greeter.GreeterBase()

     override _.SayHello req ctx =
         let resp =
-            { Greet.HelloReply.empty() with
+            { GrpcSample.HelloReply.empty() with
                 // Notice how we're immediately forced to handle missing fields.
                 // The language itself protects you from the binary protocol's quirks.
                 // How cool is THAT?
diff --git a/GrpcSample/Startup.fs b/GrpcSample/Startup.fs
index b90b0e5..4b18796 100644
--- a/GrpcSample/Startup.fs
+++ b/GrpcSample/Startup.fs
@@ -23,6 +23,5 @@ type Startup() =
             .UseEndpoints(fun endpoints ->
                 endpoints
                     .MapGrpcService<GreeterService>()
-                    .MapGet("/", (fun context -> context.Response.WriteAsync("Hello World!")))
                 |> ignore)
         |> ignore
purkhusid commented 3 years ago

I also tested the server to see if it works and it works fine. Not sure how used to gRPC you are but you can use tools like https://github.com/uw-labs/bloomrpc to test the service.