0

I have a project which ran fine as an Azure Function in v1 of the Functions runtime, but I'm busy rewriting it for v2 and I'm stuck.

My Azure Function relies on a library of mine, which in turn depends on another library. Both libraries depend on the Open XML SDK, which I've installed in those projects via NuGet. They both also have System.IO.Packaging installed via NuGet.

If I run my code through a console app, it runs fine. When I try to run it as an Azure Function, it gets partway through my library code but then the functions runtime falls over with the error:

System.Private.CoreLib: Exception while executing function: getWorkbook. ExcelGenerator: Could not load file or assembly 'System.IO.Packaging, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

I've tried adding System.IO.Packaging to the Azure Function project (which I realise I ought not to need to do) but it then falls over with a new error:

System.Private.CoreLib: Exception while executing function: getWorkbook. ExcelGenerator: Could not load type 'System.IO.Packaging.CompressionOption' from assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

I'm suspicious about the 0.0.0.0 version number in the first error, and I really don't understand the second one (or honestly why adding System.IO.Packaging to the Azure Function makes any difference at all, given that this is just running compiled library code).

One other curious thing is that WindowsBase does not show up in the "Assemblies" dependency list for my Azure Function, but it does show up checked in the "add reference" dialog box. If I uncheck it, I get a mysterious error saying "No assembly reference with the name "{0}" could be found in the project".

I'm pretty sure somehow System.IO.Packaging or WindowsBase just isn't making it through to the Azure deployment, but I'm not sure how. I'm rapidly discovering that I don't know very much about how DLL dependencies work at all let alone in Azure - any ideas?

Chris Rae
  • 5,627
  • 2
  • 36
  • 51
  • 1
    `If I run my code through a console app, it runs fine.` Is it a .Net Core console app? If so could you share how do you rewrite the function to v2, e.g the `FunctionProjectName.csproj` content? – Jerry Liu Dec 08 '18 at 00:38
  • It's a .Net Framework 4.6.1 console app. The target for my Azure portion (which is really just a very small .cs that immediately calls my library) is also .Net Framework 4.6.1. Can share anything else from the .csproj if it would help! – Chris Rae Dec 08 '18 at 20:34
  • 1
    To work with ~2 Function runtime, we need to test code with .net core target framework(`netcoreapp2.1`). And if you meet any assembly problem then, try the code in .net core console app to see whether it's a .net core problem or related to Function runtime. – Jerry Liu Dec 09 '18 at 04:10
  • I created a new console app targeting .NET Core 2.1. It didn't work initially, but I added NuGet packages for System.Configuration.ConfigurationManager and System.IO.Packaging and it now runs fine. I excitedly added a NuGet package for System.Configuration.ConfigurationManager to my Azure Functions project, but unfortunately it fails (with the second error above, the WindowsBase one). Could it be that the kinda weirdly broken WindowsBase reference in my Azure Function code is the culprit? – Chris Rae Dec 10 '18 at 20:34
  • 1
    Have you changed TargetFramework of Function project to `netcoreapp2.1`? – Jerry Liu Dec 11 '18 at 07:56
  • Oh wow. You are a life saver. This works great, and I really was completely out of ideas of what was wrong. If you are willing to write up an answer saying this I'll send through a bounty! – Chris Rae Dec 11 '18 at 19:30
  • Thanks a lot for your generosity! – Jerry Liu Dec 12 '18 at 04:52

1 Answers1

1

To rewrite v1 function to v2, right click on Function project, and Edit FunctionProjectName.csproj, change TargetFramework of Function project to netcoreapp2.1, AzureFunctionsVersion to v2.

And we may have to install packages for v2 function as they are not built-in any more as in v1.

Code refactoring is necessary, if we meet any assembly problem then, try the code in .Net Core console app to see whether it's a .net core problem or related to Function runtime.

If there're some configurations in host.json, need pay attention as some of their format has been changed.

Usually, creating a template v2 function and compare old content with new v2 template is recommended.

Jerry Liu
  • 17,282
  • 4
  • 40
  • 61