Azure Functions - Shared code across Function Apps - azure

Is there a way of sharing common code across two different Function Apps in Azure?
I understand it is possible to share code between two functions under the same Function App like so:
#load "../Shared/ServiceLogger.csx"
but I would like to share logging code between two different functions, each under it's own Function App. The reason for the functions being under two different Function Apps is that I need one to run on the Consumption plan and the other run on an App Service plan, unless there is another way of doing this?

There is no straightforward way to share code across app boundary in Azure App Sercice, and this holds true both for Function Apps and Web Apps. You will need to deploy the relevant code into each app that needs it.

What I did to get around this before the fix in VS17 Preview (which treats functions as normal cs files and allows project references) is I had a shared class project that had a post build event that would put the dlls into the Azure function projects in a folder. Then reference them with #r "file path to the dll"

One way to reuse code is to use precompiled assemblies / class libraries with your Functions. Donna Malayeri has published a blog post explaining how to achieve this: https://blogs.msdn.microsoft.com/appserviceteam/2017/03/16/publishing-a-net-class-library-as-a-function-app/
Consequently, you could have 2 or more different Functions, deployed to different AppServices or Function instances sharing the same code through a class library. Precompiled assemblies are supported in .NET C# Functions and you can do something similar using WebPack in Node.js based Functions

Related

Is it possible to publish a .net core web api to azure functions?

I've recently switched jobs from a AWS shop to an Azure shop, both using dotnet. AWS publishes Amazon.Lambda.AspNetCoreServer, which is a magic Nuget package that allows you to write a plain ol' ASP.NET core Web API and deploy it as into a lambda with only a few lines on config. I really loved this pattern because it allows developers to just write a normal web api without having the host runtime leak into their coding.
Does anything like this exist in Azure? Even something that is community supported? Or is there some any way to achieve something like this in Azure Functions?
Unfortunately there is no simple way to do that since Azure function format is a bit different.
[FunctionName(nameof(GetAll))]
public IActionResult GetAll([HttpTrigger("get", Route = "entity")]HttpRequest request)
Then it will generate json with meta data for AF.
If you wish to host pure .net core without any changes I would look into Containers option
PS0: Theoretically it would be possible to do it with little bit of reflection. For instance you create project with all your Asp.Net core apis, which you can use in asp.net core hosting. Then you write tool which grabs your dll and using reflection you find all actions in your controllers and generate code for AF
PS1: Have a look https://github.com/tntwist/NL.Serverless.AspNetCore

Multiple Azure App Function in a project

I'm currently developing a small game that will rely on a lot Azure App Functions to execute function from time to time. I followed a tutorial on MSDN (https://learn.microsoft.com/en-us/azure/azure-functions/functions-develop-vs#configure-the-project-for-local-development) explaining that I had to create a new project to host a function but so far, I already have 6 different functions and I don't really want to create 6 different projects.
Moreover, all these functions (developed in JavaScript) have a lot of code in common so I created a common JavaScript file with some helper function. Now that I have multiple projects, I can't use it anymore without copy/pasting it in all projects.
Finally, to be able to correctly develop the game, all the functions must be running in parallel on my development machine and I don't really want to open 6 (or more in the future) powershell instances to host these functions.
Is there a way to host multiple functions in the same project and deploy them easily on Azure ?
That's what Function Apps are for. Each Function App may contain multiple Functions, which will be deployed together.
You mention Javascript, but the linked tutorial is in C#. Regardless, you can put multiple functions into the same app: subfolders under the same root (where host.json file is), or static methods in the same C# project. Each function will have a separate function.json file. All functions can share the same code.

Ideally how many functions should be there in a fucntion app

As per the new template in Visual Studio 2017 prev 2, all function in a function app do share dependencies. Any custom business code/library added will be available to all other functions in a function app.
There is a problem here that it is will give monolithic design a chance to sneak in. And any change in a function would require entire set of function to be redeployed.
Another design can be to keep the function app as thin as possible (if possible one function per function app in my opinion). This would segregate the code and dependencies of each function, but on the flip side, I would bring a deployment/maintainability nightmare.
Is there any design guideline to be followed which could balance maintainability hassle and help achieving micro service design goal ?
It's up to you how much you want to group together vs be separate on their own. You can also have all your functions in 1 project in Visual Studio then deploy it to N number of function apps, then you can specify which functions a given function app should run in your host.json. Check the functions property in here https://github.com/Azure/azure-webjobs-sdk-script/wiki/host.json
Note that the Portal UI doesn't handle that correctly now, so you may get odd errors if you go in the portal. Tracked here https://github.com/Azure/azure-functions-ux/issues/1428

How to share common code among the Azure functions?

I have common code which is require in other Azure functions, how can I share the common code among the Azure functions?
If the Functions you are wanting to share code between are under the same Function App you can do the following:
https://stackoverflow.com/a/39541156/2854993
And if they are separate Function Apps, I asked a similar question - see:
Azure Functions - Shared code across Function Apps
I managed to get shared code to work (or rather: compile) using an Azure Function Tools for Visual Studio project. In case of the screenshot, the shared library is actualy another Function project. I also tried to work with class libraries: those seem to work as well.
Haven't gotten around to deploying it yet, by the way. Running (and debugging) it localy is not an issue, so I guess deploying shouldn't be one either.

Azure functions calling onto native C++

I'm designing a new architecture in Azure. It's a multi-tenant SaaS application with an ASP.NET MVC front end and some application specific data in blob storage. I need to perform some background processing on this application data at certain points. This is currently only possible using some legacy C++ code (I can't realistically rewrite this in C#).
One thought I had was to push any background jobs onto a queue and use Azure functions to service the queue as and when a job gets pushed onto it. The sticking point is the native code. I can certainly expose methods in the native code that C# can p/invoke, but can Azure functions call onto native DLLs and if so is this a sensible approach?
The code does run in a sandbox, but this approach should work. (you may want to consider exposing the relevant API in a managed assembly that would in turn be consumed by your function).
Whether you'll run into limitations with the sandbox is dependent on what your code is doing, but you can learn more about the sandbox and its restrictions here: https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox
I hope this helps!
mfcu100.dll likely depends on some other DLL inside the VC++ 2010 Redist
Most probably msvcr100.dll and msvcp100.dll.
If you include all the dependency DLLs along with your native binary, it should work.

Resources