Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions sample/gRpcSample.Client/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ open Shared
open System
open System.Net.Http
open FSharp.Control.Tasks
open Grpc.Net.Client

[<EntryPoint>]
let main _ =
HttpClientExtensions.AllowUnencryptedHttp2 <- true
task {
use http = new HttpClient (BaseAddress = Uri "http://localhost:10042")
let client = http.CreateGrpcService<ICalculator>()
let! result = client.MultiplyAsync { X = 12; Y = 4 }
use http = GrpcChannel.ForAddress("http://localhost:10042");
let calculatorClient = http.CreateGrpcService<ICalculator>()
let helloClient = http.CreateGrpcService<IHello>()

let! result = calculatorClient.MultiplyAsync { X = 12; Y = 4 }
printfn "%i" result.Result

let! r = helloClient.TestAsync { Parameter = "Saturn" }
printfn "%s" r.Response

return 0
} |> fun t -> t.Result
25 changes: 21 additions & 4 deletions sample/gRpcSample/Shared.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,30 @@ open System.Runtime.Serialization

[<DataContract; CLIMutable>]
type MultiplyRequest =
{ [<DataMember(Order = 1)>] X : int
[<DataMember(Order = 2)>] Y : int }
{ [<DataMember(Order = 1)>]
X: int
[<DataMember(Order = 2)>]
Y: int }

[<DataContract; CLIMutable>]
type MultiplyResult =
{ [<DataMember(Order = 1)>] Result : int }
{ [<DataMember(Order = 1)>]
Result: int }

[<DataContract; CLIMutable>]
type HelloRequest =
{ [<DataMember(Order = 1)>]
Parameter: string }

[<DataContract; CLIMutable>]
type HelloResult =
{ [<DataMember(Order = 1)>]
Response: string }

[<ServiceContract(Name = "Hyper.Calculator")>]
type ICalculator =
abstract MultiplyAsync : MultiplyRequest -> ValueTask<MultiplyResult>
abstract MultiplyAsync : MultiplyRequest -> ValueTask<MultiplyResult>

[<ServiceContract(Name = "Hello.Test")>]
type IHello =
abstract TestAsync : HelloRequest -> ValueTask<HelloResult>
36 changes: 22 additions & 14 deletions sample/gRpcSample/gRpcSample.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,32 @@ open System.Threading.Tasks
open Giraffe

type MyCalculator() =
interface ICalculator with
member __.MultiplyAsync request =
ValueTask<_> { Result = request.X * request.Y }
interface ICalculator with
member __.MultiplyAsync request =
ValueTask<_> { Result = request.X * request.Y }

type MyCalculatorWithDI(serializer: Json.ISerializer) =
interface ICalculator with
member __.MultiplyAsync request =
printfn "Multiply reques serialized: %s" (serializer.SerializeToString request)
ValueTask<_> { Result = request.X * request.Y }
interface ICalculator with
member __.MultiplyAsync request =
printfn "Multiply requests serialized: %s" (serializer.SerializeToString request)
ValueTask<_> { Result = request.X * request.Y }

let app = application {
no_router
listen_local 10042 (fun opts -> opts.Protocols <- HttpProtocols.Http2)
use_grpc MyCalculatorWithDI
}

type MyHelloWithDI(serializer: Json.ISerializer) =
interface IHello with
member __.TestAsync request =
ValueTask<_> { Response = $"Hello, {request.Parameter}!" }

let app =
application {
no_router
listen_local 10042 (fun opts -> opts.Protocols <- HttpProtocols.Http2)
use_grpc MyHelloWithDI
use_grpc MyCalculatorWithDI
}


[<EntryPoint>]
let main _ =
run app
0 // return an integer exit code
run app
0 // return an integer exit code
2 changes: 1 addition & 1 deletion src/Saturn.Extensions.gRPC/Saturn.gRPC.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ type Saturn.Application.ApplicationBuilder with
app.UseEndpoints (fun endpoints -> endpoints.MapGrpcService<'a>() |> ignore)

{ state with
AppConfigs = configureApp::configureGrpcEndpoint::state.AppConfigs
AppConfigs = configureApp::configureGrpcEndpoint::state.AppConfigs.[1.. state.AppConfigs.Length]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds risky - as far as I understand it'd potentially break if the use_grpc calls were not in a row.

For example, if I had in my application following code it would remove the wrong entry in state.AppConfigs:

use_grpc MyHelloWithDI
use_static
use_grpc MyCalculatorWithDI

TBF, I'm not yet sure what's the good solution here.

ServicesConfig = configureServices::state.ServicesConfig
}