1

In C#:

    private Task<bool> HandleRequest(HttpListenerContext context, CancellationToken ct)

To become in F#:

let HandleRequest (context:HttpListenerContext, ct:CancellationToken) =
    Task.FromResult(false)
//or
let HandleRequest (context:HttpListenerContext) (ct:CancellationToken) =
    Task.FromResult(false)

Now I need to invoke with

let toFunc<'I, 'T> f =
        System.Func<'I,'T> f

type FunApi() as this =
  inherit WebModuleBase()

  do
      let handle = HandleRequest |> toFunc
      this.AddHandler(ModuleMap.AnyPath, HttpVerbs.Any, handle)

But I get the error:

/..../Data.fs(57,57): Error FS0001: This expression was expected to have type    
'Func<HttpListenerContext,CancellationToken,Task<bool>>'    but here has type    
'Func<(HttpListenerContext * CancellationToken),Task<bool>>' (FS0001)
mamcx
  • 15,916
  • 26
  • 101
  • 189

1 Answers1

3

The Func requires three arguments because it has HttpListenerContext and CancellationToken as arguments and returns Task as a result.

open System.Threading.Tasks
open System.Net

//or
let HandleRequest (context:HttpListenerContext) (ct:CancellationToken) =
    printfn "%s" "Hello World"
    Task.FromResult(false)

let toFunc<'a, 'b, 'c> f =
        System.Func<'a, 'b, 'c> f

type FunApi() as this =
  inherit WebModuleBase()

  do
      let handle = HandleRequest |> toFunc
      this.AddHandler(ModuleMap.AnyPath, HttpVerbs.Any, handle)

  override __.Name = "BlaBla"


[<EntryPoint>]
let main args =
    use server = new WebServer("http://localhost:9696/")
    server.RegisterModule(new FunApi())
    server.RunAsync() |> ignore
    Console.ReadLine() |> ignore
    0
ZiggZagg
  • 1,397
  • 11
  • 16