4

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.

Janusz Nowak
  • 2,595
  • 1
  • 17
  • 36
Sam
  • 26,817
  • 58
  • 206
  • 383

2 Answers2

6

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.

Paul Batum
  • 8,165
  • 5
  • 40
  • 45
David Ebbo
  • 42,443
  • 8
  • 103
  • 117
  • Just want to make sure, through this process, I'll be able to create Azure Functions, targeting .NET Core 2.0, correct? – Sam Sep 25 '17 at 21:57
  • After installing the Azure Functions Core Tools, I created a new Azure Function in VS 2017 (15.3.5). It still seems to be targeting .NET Framework 4.6.1. Anything else I need to do to target .NET Core 2.0? – Sam Sep 25 '17 at 23:11
  • @Sam See Visual Studio section in https://blogs.msdn.microsoft.com/appserviceteam/2017/09/25/develop-azure-functions-on-any-platform/. Still a bit manual. Will get better – David Ebbo Sep 26 '17 at 01:11
1

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

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
  • What about DI support? – Sam Sep 17 '17 at 22:03
  • 2
    Actually, there is a bug that was just [fixed](https://github.com/Azure/azure-webjobs-sdk-script/issues/1792) and is being deployed. You can test it right now in West US 2, or wait for Tuesday when it should be worldwide. Try your scenarios with the fix, and please open issues on GitHub is you still encounter problems. – David Ebbo Sep 17 '17 at 22:22
  • Thanks @DavidEbbo ! Looking forward to it. Is this with WebJobs or Functions? – Sam Sep 17 '17 at 22:34
  • Functions, which is based on the WebJobs SDK, hence the term WebJobs in the repo name. Yes the naming can be a bit confusing... – David Ebbo Sep 17 '17 at 22:36
  • Great! Thanks again! – Sam Sep 17 '17 at 22:37
  • @DavidEbbo Just so I'm 100% clear, am I downloading the latest bits from GitHub? Where exactly is this bug fix? What exactly do I need to do to take advantage of latest bug fixes? I'm currently trying to build my Azure Function in VS 2017 15.3.4. – Sam Sep 18 '17 at 23:17
  • @Sam ok, now I see the problem, which is that VS itself has an old version of the functions tool. I added an Answer with a workaround. – David Ebbo Sep 19 '17 at 00:03