Execute pre-compiled .NET code as Azure Function - azure

How do I upload a pre-compiled .NET assembly(-ies) and execute my code as Azure Functions?
I'm looking for a way to run some complex domain logic, which is contained inside custom assemblies and is covered by unit tests etc.
What kind of limitations for this code are there? E.g. access remote data stores, networking etc.

Update: The below answer is still correct (still works), however there is now also first class support for precompiled functions. See the wiki page for more information on that.
The documentation (link here) describes how you can reference external libraries and Nuget packages from a C# Function using the #r syntax, e.g:
#r "System.Web.Http"
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
public static Task<HttpResponseMessage> Run(HttpRequestMessage req)
Additional details on this can be seen in this SO post.
You can also deploy custom executables and call them from your Azure Functions. For example, if you start from the Windows BAT template, you can use that to call out to an exe. Here's a sample of this in our repo, showing an image resize example. In this sample, we have a BAT script that is triggered whenever a new image is uploaded to a blob container, and the script calls out to a Resizer.exe tool to do the resize:
.\Resizer\Resizer.exe %original% %resized% 200
Regarding limitations, all Azure Functions code runs in the App Service sandbox whose limitations are described here.

To run a pre-compiled .NET assembly in an Azure Function, it's possible to upload a custom dll by FTP in the function root folder (within a bin folder) and then use #r to reference it from the azure function code.
Here is an example, a dll named "WorkOnImages.dll" is uploaded in an azure in azure function folder :
Then the dll is referenced in the azure function :
Here is the source blog post

Discouraged by the lack of Azure Function tooling support for VS2017, incompatibility with Azure SDK 3.0, I was about to throw in the towel for Functions and fallback to an approach using VS2017 and WebJobs SDK.
Then announced on March 16th, 2017, the easiest approach is documented here in an excellent blog post by Microsoft's Donna Malayeri.
It does everything I could want - true intellisense, debugging capabilities. It's been great and I wouldn't look back.

Related

Using Azure SDK for JS to create .NET 4.x App Service

I'm starting to wonder whether this is the right tool for the job, still here goes.
I'm attempting to automate the creation of our Azure Test environment using Azure SDK for JS. The environment spans many services (as you can imagine), including Classic ASP.NET app services.
Node is my safe space, so that is why I started with the JS SDK.
I have started scripting the creation of an app service using WebSiteManagementClient.webApps.createOrUpdate. I'm confused though, there is seemingly no way to configure any of the following:
Which app service plan the app service should be connected to. This feels fundamental.
The operating system, Windows or Linux.
The stack version, .NET 4.8, .NET Core, or whatever.
Is it possible to configure the above using the JS SDK, or am I going to have find another approach?
Update 23/03/21
Untested, but these are my findings so far:
App Service Plan - The plan is set using the serverFarmId property of the Site interface.
Operating system - Assuming Windows as the default, if you want a Linux app service, you change the kind property of Site from app, to app,linux.
Stack & version - In the SiteConfig interface, you have linuxFxVersion and windowsFxVersion. Again, I think the assumption is 'latest .NET' (e.g. .NET 4.8). For .NET Core 3.1, the setting looks to be DOTNETCORE|3.1.
It can be achieved using js SDK. I checked the source code and it is ok. But I don't recommend to use js sdk to do this.
Because you need to call the SDK, there are many internal logics that you need to code. This will waste a lot of your time. So I recommend you to use restapi.
The restapi method name is similar to the naming in the SDK, mainly because you can test api interfaces online to achieve the functions you want. So you can selectively choose the method you want to achieve the function you want.
Official doc
Web Apps - Create Or Update
As for your concerns, you only need to write all the configuration in json format and put it in the request body.
Tips:
First use the online interface, encode the json format, create a webapp according to your needs, and then integrate it into your code.

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

Azure Function Structure

I'm trying to wrap my head around how we're supposed to build Azure functions.
I love the idea of building serverless, compact, single-function apps that respond to events.
Here are the problems I'm running into:
I have nice class libraries built in .NET Standard 2 that handle all my "backend needs" namely handling CRUD ops with Cosmos Db, Azure Table Storage, Azure SQL, Redis, Azure Storage. No matter what I did, I couldn't integrate these class libraries into an Azure Functions project. More details below.
Also, getting dependency injection in Azure Functions project has proven to be quite a task -- especially with my class libraries mentioned above.
At this point, the only option I'm seeing is to "copy and paste" code into a new Azure Functions project and use it without any DI.
This seems to go against "best practices". So what's the solution other than either to create monolithic code or wait till Azure Functions support .NET Core and DI.
I thought I could use my .NET Standard class libraries from a regular Azure Functions project targeting .NET Framework. After all, the idea of .NET Standard is to "standardize" things. I opened a couple of posts here on SO. I'm providing the links so that you can see the issues I've run into:
Using .NET Core 2.0 Libraries in WebJob Targeting .NET Framework 4.7
No parameterless constructor error in WebJobs with .NET Core and Ninject
P.S. My previous posts are referring to WebJobs. That was plan B approach because WebJobs seem half a step ahead of Azure Functions when it comes to supporting things like .NET Core and DI. Ultimately, I'd like to build a few Azure Functions that can use my class libraries built in .NET Standard 2.
Also, my previous posts mention that my class libraries target .NET Core 2.0. Since then I converted them to .NET Standard 2 which didn't really take much at all. I did this so that I truly conform to .NET Standard 2.
One issue is that Visual Studio has an outdated version of the Functions Core tools. Until this is resolved, you can work around in the following way:
Install the latest via npm by running npm install -g azure-functions-core-tools
In your Function App in VS, go to the Properties
Go to Debug, and click New... under Profile
Name the new Profile something like FunctionsNpm
Set the executable to (replace [YourUserName]): C:\Users\[YourUserName]\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\func.exe
Set the arguments to host start
Set the working directory to $(TargetDir)
In toolbar, look for the green triangle icon to change your current Profile to the one you just created:
Now when you run from VS, you'll be using the npm tools instead of the older one that come with the VS package.
.NET Standard 2 support is on its way, see this github issue.

Azure Functions - Shared code across Function Apps

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

Run DLL on Azure Cloud Service

I've worked a lot with Azure websites but not cloud services. I need to spin up a cloud service that uses a third party dll to generate pdfs (http://wkhtmltopdf.org/). However, I would like to write this server in nodejs and almost all examples are in asp.net. I just need to know how to reference and execute this dll (or exe, if that's easier) from node js in an Azure Web Role when a request comes in. I'd greatly appreciate any pointers.
Thanks,
Sam
EDIT: It's not necessarily a matter of referencing or using the dll from the code that I will have an issue with. It's how to INSTALL the dll on an Azure web role. All you can do in an Azure cloud service / web role is upload the project package but no explanation on how to add a dll or exe (except in asp.net, which I am not using. I'm using nodejs).
You can put the installer in your web role package, and set up a startup task in the ServiceDefinition.csdef file for the roles.
Here is the sample on official site Install .NET on a Cloud Service Role, which is guiding to install .NET in Cloud service, the steps and scenario should be the same.
There are a lot of wrappers for wkhtmltopdf. This one is nice and pipable (you can stream the output directly to your client):
var wkhtmltopdf = require('wkhtmltopdf');
// URL
wkhtmltopdf('http://google.com/', { pageSize: 'letter' })
.pipe(fs.createWriteStream('out.pdf'));
// HTML
wkhtmltopdf('<h1>Test</h1><p>Hello world</p>')
.pipe(res);
You have to install wkhtmltopdf (the command line tool) first of course.
Assuming that the dll is a managed assembly (i.e. an assembly compiled using .net), you can use edge.js to get NodeJS to interact with the dll.
If it is an exe, you can execute the process via NodeJS's asynchronous process creation API.

Resources