Last week I looked on how you could create an Excel file in Logic Apps and then
e-mail that file to someone. This week I solved the same problem in Azure
Functions. In retrospect, I think this was a better solution.
Strategy
Creating Azure Functions in C# is straightforward. Often you could use different
kind of bindings so you could spend less time on infrastructure code. For
instance, you could define something to be stored in a blob storage, but without
working with some blob storage API. However, in this solution I could not use
any binding.
The solution is quite simple, and there are just three major steps.
Creating a player list
In the first step of the application a list of players is created. This is then
used to create the Excel file. First, I hard coded this. But that was to easy
:-) So I started to investigate how you could get the data from a database
instead.
Normally you use Entity Framework or something similar when you are working
against a database. And this is also an option when you work with Azure
Functions, this totally works.
But it looks to me that many are using the classic
ADO.NET
instead when they are working with Azure Functions. This is very primitive, and
you will work SQL strings in your C# code. For simple solutions I find this to
be good enough. You just need to be careful when you make schema changes to your
database.
Creating an Excel file
In the logic app it required several steps to generate an Excel file. Also, it
required access to a OneDrive account and the generating was very slow.
In this solution, I started by installing the NuGet package
ClosedXML. Then it just took me a
few lines of code to create a file with the data.
The code runs super-fast. Also, I do not have any extra spreadsheets and I could
auto adjust auto the column widths in the created table. These were problems
that were hard to solve in the Logic App solution.
E-mailing the file
In the last step the generated file should be send by e-mail to someone. I think
the Logic App had a simpler solution for this.
In Azure Functions, there is a
SendGrid
binding you could use to send e-mails. I have not tried that, but I guess it
should not be too hard to use.
But I needed to send e-mail via Office365. There is no binding for that, so I
fallback to SMTP instead. There is a couple of settings to configure, and the
code is pretty long but it is nothing complicated.
Solution
Here is all the code for the application:
Azure Function Solution
And here is the settings file (local.settings.json)
local.settings.json
Summary
This was a fun comparison to make. Both solutions have their own strengths and
weaknesses.
I think it was easier to fetch data and generate an Excel file in the Azure
Functions solution. And it was way more efficient. But when it comes to send
e-mail, at least via an Office account, the Logic app was easier.
If I must pick one solution, I think the Azure Functions solution overall was a
better.
But it is totally possible to mix these solutions. You could use the Azure
Function to just generate a file in Azure Blobs storage, there are binding ready
to that. And then you could have a Logic App that is triggered when blobs are
created, and it could be used to e-mail the file.