Why does my Azure V2 time function crashes with newtonsoft reference?

0

I have a simple timer based azure function that crashes with the following message

I have added the nuget package for newtonsoft.Json so I am not sure why this is a problem.

[1/11/2018 07:00:26] Executed 'PimDataFeeder' (Failed, Id=291e9147-7f57-4fd3-887d-a8001afc8230)
[1/11/2018 07:00:26] System.Private.CoreLib: Exception while executing function: PimDataFeeder. System.Private.CoreLib: Could not load file or assembly 'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly 'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.

---- EDIT ----

The simple function looks like this, basically it downloads a file from a remote destination and manipulates it in memory before writing it into a CosmosDB instance, or at least thats the idea once it starts working. Putting a break point in the loop tells me that the first loop iteration works and indeed the first line from the string is split properly and then follows the crash

using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using System.Net.Http;
using System.Net.Http.Handlers;
using System.Net.Http.Headers;
using System.IO.Compression;
using System.IO;
using System.Text;
using System.Net;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;

namespace NWCloudPimDataFeeder
{
    public static class PimDataFeeder
    {
        [FunctionName("PimDataFeeder")]
        public static async System.Threading.Tasks.Task RunAsync([TimerTrigger("0 */15 * * * *")]TimerInfo myTimer, TraceWriter log)
        {
            // The endpoint to your cosmosdb instance
            var endpointUrl = "https://example.com";
            // The key to you cosmosdb
            var key = "XXX";
            // The name of the database
            var databaseName = "XXX";
            // The name of the collection of json documents
            var databaseCollection = "XXX";

            log.Info($"C# Timer trigger function executed at: {DateTime.Now}");

            HttpClientHandler handler = new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            };

            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Add("Authorization", "Bearer XXX");

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage file = await client.GetAsync("https://example.com");
            var content = await file.Content.ReadAsByteArrayAsync();

            MemoryStream originalFileStream = new MemoryStream(content);
            using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
            {
                MemoryStream decompressedFileStream = new MemoryStream();
                decompressionStream.CopyTo(decompressedFileStream);
                byte[] fileResult = new byte[decompressedFileStream.Length];
                decompressedFileStream.Position = 0;
                decompressedFileStream.Read(fileResult, 0, fileResult.Length);
                string result = System.Text.Encoding.UTF8.GetString(fileResult);
                //log.Info(result);

                foreach (var singleItem in result.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries))
                {
                    log.Info("singleItem looks like: " +singleItem);
                    log.Info("In the loop");
                    var itemWrapper = new ItemWrapper { NWID = Guid.NewGuid(), Item = singleItem, DocumentType = "Item"};

                    // Create a cosmosdb client
                    using (var docClient = new DocumentClient(new Uri(endpointUrl), key))
                    {
                        // Save the document to cosmosdb
                        docClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(databaseName, databaseCollection), itemWrapper)
                            .GetAwaiter().GetResult();
                    }
                }
            }
        }
    }

    public class ItemWrapper
    {
        public Guid NWID { get; set; }
        [Required]
        [JsonProperty("item")]
        public string Item { get; set; }
        [Required]
        [JsonProperty("documentType")]
        public string DocumentType { get; set; }
    }
}
azure
json.net
azure-functions
asked on Stack Overflow Nov 1, 2018 by Matt Douhan • edited Nov 5, 2018 by Jerry Liu

2 Answers

1

Right now the Cli and Runtime is output as below when we debug function in VS. And the function project is created with Microsoft.NET.Sdk.Functions 1.0.23(>=1.0.14) references Newtonsoft.Json 11.0.2 by default.

Azure Functions Core Tools (2.1.748 Commit hash: 5db20665cf0c11bedaffc96d81c9baef7456acb3)
Function Runtime Version: 2.0.12134.0

I can only repro the problem with some old Function runtime, which still requires v10 Newtonsoft.Json. So check Function runtime version and make sure VS consumes the latest.

Download and set cli manually

  1. Delete old Function CLI using by VS. Remove subfolders under %localappdata%\AzureFunctionsTools\Releases.
  2. Delete template engine consumed by VS %userprofile%\.templateengine.
  3. Go to CLI feed to download latest cli, right now it's feed 2.10.1 and CLI 2.1.748.
  4. Go to %localappdata%\AzureFunctionsTools\Releases and create folder 2.10.1.
  5. Decompress the zip and rename it to cli, drag it under 2.10.1.
  6. Copy templates folder under cli to 2.10.1, and rename two files inside by removing version. e.g itemTemplates.2.0.0-10300.nupkg to itemTemplates.nupkg.
  7. Create a manifest.json under 2.10.1 as below and change username.

        {
          "CliEntrypointPath": "C:\\Users\\UserName\\AppData\\Local\\AzureFunctionsTools\\Releases\\2.10.1\\cli\\func.exe",
          "FunctionsExtensionVersion": "~2",
          "MinimumRuntimeVersion": "2.1",
          "ReleaseName": "2.10.1",
          "RequiredRuntime": ".NET Core",
          "SdkPackageVersion": "1.0.23",
          "TemplatesDirectory": "C:\\Users\\UserName\\AppData\\Local\\AzureFunctionsTools\\Releases\\2.10.1\\templates"
        }
    

The folder structure should be like this

enter image description here

After restarting VS, everything should work as expected.

answered on Stack Overflow Nov 2, 2018 by Jerry Liu • edited Nov 2, 2018 by Jerry Liu
0

These errors are often times caused by not having the latest version of the Functions SDK/Tools installed. The reference to Newtonsoft 11.0.0 suggests that this might be the be the case (latest version is 11.0.2). You can navigate to Tools -> Extensions and Updates -> Updates (bottom left of window) in VS to check for updates to Azure Functions tooling. Once you have the latest update, trying re-creating the function to see if it works.

answered on Stack Overflow Nov 1, 2018 by Maheer Iqbal

User contributions licensed under CC BY-SA 3.0