Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newtonsoft 11.0.0.0 unable to load on Azure Function App 2.0

I created a out of the box Azure Function App with an Azure Http Trigger. Which gave me the below code. All I have updated is I am converting the HttpRequest body into my Helper class.

Here is the code

public static class TriggerTest
{
    [FunctionName("TriggerTest")]
    public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
    {

        log.Info("C# HTTP trigger function processed a request.");

        string name = req.Query["name"];

        string requestBody = new StreamReader(req.Body).ReadToEnd();

        Helper data = JsonConvert.DeserializeObject<Helper>(requestBody);

        name = name ?? data?.value;

        return name != null
            ? (ActionResult)new OkObjectResult($"Hello, {name}")
            : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
    }
}

public class Helper
{
    public string value { get; set; }
}

When I attempt to run it compiles fine, but then the console is spammed with the below

A ScriptHost error has occured

System.Private.CoreLib: Exception while executing function: TriggerTest. TestingAzure.FunctionApp: 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’

All of the Nuget packages are referencing Newtonsoft 11.0.2 which is what the Microsoft.NET.Sdk.Functions referencing. The project is a .NET Standard 2.0 project. The Nuget packages I am referencing are

  • Microsoft.ApplicationInsights v2.7.2
  • Microsoft.Azure.WebJobs.Extensions v3.0.0.-beta8
  • Microsoft.Azure.WebJobs.Extensions.Http v3.0.0-beta8
  • Microsoft.Azure.WebJobs.ServiceBus v3.0.0.-beta5
  • Microsoft-NET-Sdk-Functions v1.0.19 NETStandard,Library v2.0.3
  • Newtonsoft.Json v11.0.2

I am running this locally and have not yet tested it up in Azure, however I need it to work locally for testing purposes.

Also the CLI which is downloaded from Visual Studio 2017 is 2.0.1-beta.25

Azure Functions and Web Jobs Tools for Visual Studio is Version 15.10.2009.0

like image 906
Canvas Avatar asked Sep 07 '18 11:09

Canvas


1 Answers

For v2 functions, Function sdk 1.0.19(>=1.0.14) references Newtonsoft.Jon v11.0.2 by default. The error results from the CLI your VS uses. 2.0.1-beta.25 is too old, the latest in VS is 2.0.1-beta.38 right now.

Solution is to make sure the download succeed. Besides, Microsoft.Azure.WebJobs.ServiceBus should be 3.0.0-beta8.

  1. Delete old CLI folder %localappdata%\AzureFunctionsTools.

  2. Restart VS and create a new Azure function. Wait at the creation dialog for VS to download new CLI and templates, until we see the tip change to Updates are ready.

    enter image description here

    enter image description here

  3. If you don't see Update are ready after a while(time to download 200M files on your side), check %localappdata%\AzureFunctionsTools again. If only folder 2.0.1-beta.25 is filled with content, try to repeat steps above or resort to update part of this answer to download manually, need to visit %localappdata%\AzureFunctionsTools\feed.json to find download url of latest version(feed version=2.5.2 right now).

like image 93
Jerry Liu Avatar answered Sep 17 '22 15:09

Jerry Liu