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: 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!