Creating a fully functional F# microservice (Quartz.Net, Net.Mail)

Recently I realized that I have the data where users evaluate things presented to them. These opinions are stored in the database, but let's be honest how often you open a management studio to see what records appear in the database? I am definitely not that kind of person. So I thought it would be fun to write an application that would sent as a weekly report and also providing an api, to ask for data from the last week. In this article we would take a look at scheduling mails on weekly basis.

March 11, 2017 - 3 minute read -
F# Microservices Patterns


In previous posts we learned how to create a project in Nancy F#. How to configure Fsharp.Configuration, SqlTypeProvider and how to use the Azure Api A. As you probably remember mikroservie had in his intention to send periodically e-mails with information about what new evaluations appeared and how their sentiment looks like. In order to achieve this purpose I decided to use the Quartz.Net library for job scheduling and System.Net.Mail to send e-mail messages. I’ll start with the configuration of the Quartz.Net, starting with adding following record to paket.dependecies file: We create a new file in which we place the code of our task. We start by creating a class that will inherit IJob interface. This class should implement the Execute method and perform the action that should be performed periodically. In our case, this method will look like this: As we can see, first we get data, then we ask azure about sentiment and then send e-mails(details about sending emails in a moment). Then I decided to create a new module a job factory. So by we could easily schedule a job in main.fs and keep (main.fs) free of Quartz namespaces. The function looks like this: As we can see in the method schedulejob we create an SchedulerFactory object, create a job of type previously created by us type(“Job”) and define starts periodicity. Where periodicity is defined by the following method:

As you can see periodicity is set that our action is invoked every week. Well, we have created Job class and its factory, we should call somewhere scheduleJob method from a fabric. So we add method invocation to a main.fs file: And its all. At this point we have done task scheduling. The final aspect worth explaining is mail sending that left for the end. To send mail I used a System.Net.Mail library. I started by creating a new file and place there a new class(MailSender). This class contains a single method that is available outside, a send method. It is designed based on input data to build a body of the message and then send it from/to email defined in a configuration file. Sending method looks like this: Method for creating a message like this: An interesting fact here is setting observer Who has various “tasks” to be performed when an error occurs (for example printing an error msg to console and above all call a Dispose method) Of course, we will add new endpoint (method in nancy module) which allows sending e-mails. It looks in the following way: result Summarize all posts we can see how easily we could create a small microservice that contains a couple of functionalities. Comparing it to do similar/related service what I created in C# I could say that purity of code due to its greater brevity is way better. We also eliminate a lot of redundant lines of code (boilerplate code) and the complexity in code, which is often imposed by the selected technologies (dependency injection configuration, database configuration, etc.). In my opinion this example very nicely shows how we can “facilitate” Creating a microservices by using F#, in comparison to making them in C#.

Thanks for reading!