Build web service using F# and ASP.NET Core

Samuele Resca
Apr 15, 2018 · 4 min read
Image for post
Image for post

The aim of following article is to build web service using F# and ASP.NET Core using the Giraffe library. Therefore, we’ll build some JSON APIs which takes data from a data source, and exposes them through HTTP.

Before start, I invite you to read this article about Web development in F#.

You can find the following demo on github: https://github.com/samueleresca/Blog.FSharpOnWeb

Setup the project

Setup a Giraffe project is very simple. First of all, let’s start by adding giraffe projects template:

The previous command install some project templates in order to build Giraffe web applications. Afterwards you can create a new Giraffe application by running .

The command will create a project with some files inside it. The most important is file, which contains the setup of the host, the routing engine and services:

Project structure

As ASP.NET MVC developer, I am used to follow an naming-convention structure in order to keep separated all the components of my application: Views, Models, Startup, Routing. Giraffe simply keeps all these implementations inside the file.

Let’s create a more structured project. In that case, I will proceed with the following structure:

  • contains all the model which will reflect the data source structure;
  • defines the db context and it implements all abstractions over data manipulation in order to access to our data source;
  • implements types which define the DTOs models;
  • handler accepts the type and retrieve informations from functions;
  • it is very similar to file into an ASP.NET Core project: it defines services, web host and routing;

Access data through Entity framework core

EF Core is the “official” ORM shipped with ASP.NET Core. I have already talk about EF Core in the following articles: Implementing SOLID REST API using ASP.NET Core, Developing token authentication using ASP.NET Core. Obviously, EF Core is just an option, you may replace it with your favourite implementation or ORM.

EF Core APIs are the same between C# and F#, therefore it will force you to fit your F# code to C# APIs.

In order to proceed, we need a model which will be contained into and a data context, which will be implemented into . Let's start by defining the model:

All the models needs to expose the attribute, which allows EF Core to threat them as mutable object only @ compiler level.

The file will contain the . It will extend the base and it will define all the DbSet used by repository module:

Finally, we will define the module in order to perform data manipulation. Each method will return an type in order to handle data source exception case.

Getting started with HttpHandler

Let’s start with some fundamentals of Giraffe framework. The main building block in Giraffe is a so called :

A is a function which takes two arguments: , , and it returns a (wrapped in an and workflow) when finished. On a high level a function receives and returns an ASP.NET Core object.

can process an incoming before passing it further down the Giraffe pipeline by invoking the next or short circuit the execution by returning an option of .

Each handler is mapped to an URL through the route configuration of Giraffe.

Let’s see a concrete example inside our implementation. The following routing objects shows a mapping between some urls: , and some handlers: , :

The url will retrieve all the labels from database, and the will retrieve a label by id. file contains implementations of handlers:

Manage CRUD operations using HttpHandler

Handler functions are the bridge between the http client and our application. As seen before, the file implements the methods, which retrieve informations from our datasource.

Let’s continue by implementing all CRUD operations in our :

and implement respectively the add operation and update operation. Each of them retrieve the by using the method.

method is part of the dependency management system of ASP.NET Core, which works out of the box with Giraffe.

The services, and in general, all services are defined into the file:

In that case, we are configuring a new by passing the connection string.

Model validation

Giraffe also implements out-of-box a model validation system. The validation criteria can be defined into models. Indeed, in our example, we add an additional level by defining the file.

Here is the implementation of :

The method can be used into handlers in order to check the consistency of the request and provide some feedback to the client:

Final thought

Finally, we can perform operations over data by using the following calls

This example shows you some how to build build web service using F# and ASP.NET Core, focusing on some features provided by Giraffe framework, and it is a good deep dive into web functional world for all software engineers who comes from an OOP approach.

This article is NOT an invitation to move all your code to FP paradigm. If you think different approaches, like FP and OOP, are mutually exclusive, then I invite you to read this article: OOP vs FP.

The example is available on github.

Cover photo credits: Graz — Mur Island

Cheers :)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store