BT

Your opinion matters! Please fill in the InfoQ Survey!

ASP.NET Core and F# with Giraffe

| by Pierre-Luc Maheu Follow 2 Followers on Oct 23, 2017. Estimated reading time: 1 minute |

Giraffe is an F# micro web framework for building web applications. It sits on ASP.NET Core, providing an F# API to the web framework. Giraffe is intended for developers who want to build web applications in F# while retaining access to the features of ASP.NET Core and its ecosystem.

The syntax of Giraffe is similar to Suave, a popular F# web framework. The similarity prompted questions about whether the two frameworks should be either merged or share a common set of APIs. The creator of Giraffe explains why he doesn’t believe merging would add much value:

One of the main differences between Giraffe and Suave is that Giraffe’s fundamental vision, as often stated, is to tightly integrate with ASP.NET Core. It was born to fill the niche where functional .NET devs wanted to build a functional ASP.NET Core web application. Giraffe tries to fill this specific niche by providing a thin layer on top of ASP.NET Core, but keeping the core building blocks of ASP.NET Core alive (DI, HttpRequest, HttpResponse, Config, etc.). This enables F# developers to use a lot of the already existing (and future upcoming) ASP.NET Core ecosystem.

The main building block of Giraffe is HttpHandler. An HttpHandler is a pipeline of functions, similar to ASP.NET Core composition through IApplicationBuilder. The handler can continue the pipeline processing by calling the next handler.

type HttpHandler = HttpFunc -> HttpContext -> HttpFuncResult

Giraffe uses a combinator approach, where HttpHandlers are combined together to create higher level abstractions and ultimately create an application.

let webApp =
choose [
route “/foo” >=> text “Foo”
route “/bar” >=> text “Bar”
]

type Startup() =
member __.Configure (app : IApplicationBuilder)
                    (env : IHostingEnvironment)
                    (loggerFactory : ILoggerFactory) =
    app.UseGiraffe webApp

Giraffe uses .NET Task instead of async workflows, as the two implementations are different and require to convert from each other. Giraffe minimizes conversions that way to reduce overhead.

let personHandler =
    fun (next : HttpFunc) (ctx : HttpContext) ->
        task {
            let! person = ctx.BindModel<Person>()
            return! json person next ctx
        }

Example applications are available on GitHub.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT