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
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
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
.
Delete old CLI folder %localappdata%\AzureFunctionsTools
.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With