TL;DR; You should really update your Nuget packages
I am currently experimenting a lot with Azure due to my new job and also of course due to the really cool innovation that we can find there. Recently I got interested in Azure Functions. This cool feature allows to write a small piece of code that runs on Azure and can be triggered by various events (time based, HTTP-request based, etc). For example, imagine that you have a value that keeps changing and you want to monitor this value and do some kind of analytics on this. In the code I am currently writing, I am using the value of a Bitcoin as the sample, and I am just periodically reading this value and storing it into an Azure database.
For this small functionality, it would be much too complicated to set an entire server-based application up. This is a perfect example for a time-based Azure Function. I am not going to explain more about Azure Functions here, because I am working on a complete article including sample which will do that. But in the course of my investigation, I stumbled upon an issue that can easily be solved.
The issue arises when you try to write unit tests for your Azure Function. In many examples I saw, the Azure Function was created straight in the Azure portal, which is great but makes it a bit difficult to unit test the code. Thankfully it is also possible to create the Function project in Visual Studio, and to take advantage of all the features including unit test, code coverage etc.
To do this, try the following:
- Install Visual Studio 2017. I currently have the Update 3, which is the most recent at this time. Make sure to select the Azure workload when you install Visual Studio!
- In Visual Studio, select Create new project.
- In the New project dialog, select Cloud on the left, and then Azure Functions.
- Give your project a name and then click OK.
- Right click on the Function project and select Add, New Item.
- In the Solution Explorer, right click on the solution and select Add, New Project.
- In the Add New Project dialog, select the Test category, and then Unit Test Project (.NET Framework).
- Give your unit test project a name and then click OK.
- In the Unit test project in Solution Explorer, right click the References folder and select Add Reference.
- In the Reference Manager dialog, select Projects and then the Function project that you created earlier. Then click OK.
- Build the solution.
If you have the same setup as I have, you will get an error stating something like:
Metadata file ‘C:\Users\Laurent\Documents\Visual Studio 2017\Projects\FunctionApp2\FunctionApp2\bin\Debug\net461\FunctionApp2.dll’ could not be found
Indeed, if you check the path in Windows Explorer, you will see that there are no DLLs under FunctionApp2\bin\Debug\net461. There is however a folder under FunctionApp2\bin\Debug\net461\bin which contains all the DLLs. The issue is that the Functions project does not generate its output to a standard path. As a result, the Unit test project does not find the reference it was expecting.
After reaching out to the Azure Functions team, I found out that this is a know issue and that an updated project template for Visual Studio will be released soon. But there is already an easy fix that you can apply to your project today: You just need to update your Nuget packages, and this will apply the fix to the Functions project.
- In the Solution Explorer, right click on the Solution and select Manage Nuget Packages for Solution.
- Under Installed, select the Microsoft.NET.Sdk.Functions package.
- On the right under Versions, select the Function project. Note that the version shown is smaller than 1.0.2 (likely 1.0.0).
- Below this box, make sure that Latest stable 1.0.2 is selected in the Version combo box and then click Install.
- Build the project again. This time it should succeed.
This was an easy fix that the Azure Functions team was able to deploy to your project using an update to the Nuget package. Of course, there are good reasons why teams are sometimes reluctant to apply an update: They might fear that the update brings breaking changes, or that something might go wrong unexpectedly due to regression bugs etc. But when you create a brand new project, it is a good idea to apply the Nuget updates to the solution. This way you will make sure that your new project starts with the latest and greatest.
Thanks to the Azure Functions team for their help solving this issue!