ServiceStack: F# Client Application

In the previous post “ServiceStack: New API – F# Sample (Web Service out of a web server)” we implemented a self-hosted service with ServiceStack. That service has multiple out-of-the-box endpoints, including a REST one.

The next interesting question is “How to call this service?”(preferably in a strongly-typed way). The answer is simple, ServiceStack team have already made ​​this for us.  We can reuse types that designed for server-side code to make client code prettier. ServiceStack provides a list of different service clients for client applications.

open System
open ServiceStack.ServiceHost
open ServiceStack.ServiceClient.Web

[<CLIMutable>]
type HelloResponse = { Result:string }

[<Route("/hello")>]
[<Route("/hello/{Name}")>]
type Hello() =
    interface IReturn<HelloResponse>
    member val Name = "" with get, set

let baseUri = "http://localhost:8080/"

// Option 1 : Json call
let jsonCall() =
    let client = new JsonServiceClient(baseUri)
    client.Post(Hello(Name="json"))

// Option 2 : Xml call
let xmlCall() =
    let client = new XmlServiceClient(baseUri)
    client.Post(Hello(Name="xml"))

// Option 3: Jsv call
let jsvCall() =
    let client = new JsvServiceClient(baseUri)
    client.Post(Hello(Name="jsv"))

[<EntryPoint>]
let main args =
    printfn "Json call : %A" (jsonCall())
    printfn "Xml call : %A" (xmlCall())
    printfn "Jsv call : %A" (jsvCall())
    Console.ReadLine() |> ignore
    0

2 thoughts on “ServiceStack: F# Client Application

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s