Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  1. 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.
  2. 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.

like image 303
Sam Avatar asked Mar 08 '23 01:03

Sam


2 Answers

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: enter image description here

Now when you run from VS, you'll be using the npm tools instead of the older one that come with the VS package.

like image 139
David Ebbo Avatar answered Mar 29 '23 15:03

David Ebbo


.NET Standard 2 support is on its way, see this github issue.

like image 26
Mikhail Shilkov Avatar answered Mar 29 '23 15:03

Mikhail Shilkov