C# Azure Function for generating an ICS File

Last updated: 20-06-2017
Edit on GitHub

This is a sample HTTP trigger Azure Function that returns a ICS file that to the client. The project takes a dependency on iCal.NET, but demonstrates how a simple HTTP Trigger can be implemented.

Additional Details and History

This project originally came out a need to create an ICS file for the Azure Functions webinar. Armed with only the knowledge of what I wanted to create, below is how I started investigating how to do this with Azure Functions :

I began by taking a look at a third party library called iCal.NET. It looks like this handles all the various time zones and client. Next, I opened the Azure Portal and creating a new Azure Function application securing the name that folks would go to :

Next, I chose a language and happily picked C# and used a HttpTrigger template.

By default, the code in the template is looking for a name parameter and a POST command. Since, I didn't want to respond to a POST, but rather just a GET, I used this as my start but deleted the name parameter section.

Armed with just the online editor, I started writing code. I could have downloaded Visual Studio 2017 Preview 3 and the Azure Function tooling for a richer debugging experience, but it was getting late and I wanted to crank this out with just a browser and my C# skills.

I needed to pull in a third party library, so I noticed that you could view "Files" inside the portal and that it supports NuGet. I simply created a project.json and added the following NuGet package reference to iCal.NET and Newtonsoft.Json :

{
  "frameworks": {
    "net46": {
      "dependencies": {
        "Ical.Net": "2.2.19",
        "Newtonsoft.Json": "9.0.1"
      }
    }
   }
}

When I switched back to my function, I noticed my project started pulling down the references and restarted my function.

The code is pretty self-explanatory if you've used iCal.NET before, but basically I create the file and ensure when someone visits the site that it sends down an ICS file named webinarinvite.ics.

During the process of working with the function, I hit the "Save" button and it would let me know if it compiled successfully or not. It finally compiled successfully as shown below :

2017-06-06T23:19:02  Welcome, you are now connected to log-streaming service.
2017-06-06T23:20:02  No new trace in the past 1 min(s).
2017-06-06T23:23:04.794 Function started (Id=xxx-1d71-4f1f-8fbd-6d1b4dcc96f0)
2017-06-06T23:23:05.921 Function completed (Success, Id=xxx-1d71-4f1f-8fbd-6d1b4dcc96f0, Duration=1137ms)

Very cool! Now to test the function, it was as simple as selecting GET from the dropbox on the Test Tab and hitting the Run button.

Now that my function was working properly, I grabbed the URL to my app and pasted it into the browser. The URL was easy to find as shown below:

Success! It downloaded the .ICS file and after opening it, the following entry appeared in my calendar :

I've recently checked the traffic using the "Monitor" section and noticed it has already received 393 downloads!

Whenever I need to update this in the future, all it will require is changing two lines. No additional servers, redeployment, etc. is needed as it will auto compile.

Btw, you can try it now at http://aka.ms/AzureFunctionsLive and be sure to follow @AzureFunctions on Twitter!

Contributing

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.