Azure Functions v2
C#
From https://github.com/dotnet/corefx/issues/41534 (System.Text.Json: Method not found System.Text.Encodings.Web.TextEncoder.FindFirstCharacterToEncodeUtf8)
Works perfectly well in every project _except_ Azure Functions (v2).
Simply HTTP trigger an Azure Function and try and serialize (or deserialize) an object.
cc @electricessence, @fabiocav
Is there a workaround to this dependency version pinning issue?
This issue looks similar to https://github.com/Azure/azure-functions-host/issues/4906.
Here's a repro with a similar error:
System.Private.CoreLib: Exception while executing function: Function1. TestDependency: Method not found: 'System.Text.Encodings.Web.JavaScriptEncoder System.Text.Encodings.Web.JavaScriptEncoder.get_UnsafeRelaxedJsonEscaping()'.
```csproj
```C#
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
var output = new MemoryStream();
using (var writer = new Utf8JsonWriter(output,
new JsonWriterOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }))
{
writer.WriteString(Encoding.UTF8.GetBytes("name"), "Bob1");
writer.Flush();
}
name = System.Text.Json.JsonSerializer.Serialize(Encoding.UTF8.GetString(output.ToArray()));
return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
}
Http Functions:
Function1: [GET,POST] http://localhost:7071/api/Function1
[10/7/2019 3:09:01 AM] Host lock lease acquired by instance ID '000000000000000000000000B068BFBB'.
[10/7/2019 3:09:07 AM] Executing HTTP request: {
[10/7/2019 3:09:07 AM] "requestId": "a37a95c1-868d-484d-82d9-f9cc6e34ba3b",
[10/7/2019 3:09:07 AM] "method": "GET",
[10/7/2019 3:09:07 AM] "uri": "/api/Function1"
[10/7/2019 3:09:07 AM] }
[10/7/2019 3:09:07 AM] Executing 'Function1' (Reason='This function was programmatically called via the host APIs.', Id=c9432a44-ea43-4ad9-b45a-7b642c43f07c)
[10/7/2019 3:09:08 AM] Executed 'Function1' (Failed, Id=c9432a44-ea43-4ad9-b45a-7b642c43f07c)
[10/7/2019 3:09:08 AM] System.Private.CoreLib: Exception while executing function: Function1. TestDependency: Method not found: 'System.Text.Encodings.Web.JavaScriptEncoder System.Text.Encodings.Web.JavaScriptEncoder.get_UnsafeRelaxedJsonEscaping()'.
[10/7/2019 3:09:09 AM] Executed HTTP request: {
[10/7/2019 3:09:09 AM] "requestId": "a37a95c1-868d-484d-82d9-f9cc6e34ba3b",
[10/7/2019 3:09:09 AM] "method": "GET",
[10/7/2019 3:09:09 AM] "uri": "/api/Function1",
[10/7/2019 3:09:09 AM] "identities": [
[10/7/2019 3:09:09 AM] {
[10/7/2019 3:09:09 AM] "type": "WebJobsAuthLevel",
[10/7/2019 3:09:09 AM] "level": "Admin"
[10/7/2019 3:09:09 AM] }
[10/7/2019 3:09:09 AM] ],
[10/7/2019 3:09:09 AM] "status": 500,
[10/7/2019 3:09:09 AM] "duration": 2444
[10/7/2019 3:09:09 AM] }
Looks like the .NET Core 2.2 dependencies are overriding the newer ones that the user code might bring in/rely on:
https://github.com/Azure/azure-functions-host/blob/a63aeed826c4a1ee7c4e8c676bf7557c88a86ba1/src/WebJobs.Script.WebHost/WebJobs.Script.WebHost.csproj#L53-L66
For example: Microsoft.AspNetCore.App version 2.2.7 brings in the version 4.0.3.0 of the assembly where-as the version that S.T.Json depends on is 4.0.4.0 (this is the version that ships in-box with .NET Core 3.0 and is available as part of the latest 4.6.0 NuGet package).
The function output directory has the right dlls:

However, Azure Functions is loading the older dll first (that comes built-in with the azure functions cli):


Azure/app-service-announcements#200
Thanks for looking into this. Is there any work around for S.T.J. currently? Migrating to Azure Fn v3 is an option, but we are currently blocked by this issue.
Az Host pins versions of specific assemblies and will always lag behind latest releases by some amount. 2.2 is what compatible with Function Runtime Version: 2.0.12673.0
Self host in a container is always an option.
@espray I can't even get this to work locally on my dev machine? How might I proceed?
Is there some assembly hack I can put in to reference the proper STEW package version?
I think your only option for core 3 right now, build v3.x branch and host it container. No clue how stable v3.x branch is.
We'll have a preview (with a supporting version of the CLI) available very soon. Please keep an eye on the announcement @espray linked above as we'll update it once those bits become available.
@fabiocav thanks for the update. Do you have an 'earliest' vs 'latest' timeframe?
Next Azure Functions Live is scheduled for the 10th
https://www.youtube.com/watch?v=E_xcnwNONZc
cc @terrajobst
:( (sigh) Live rescheduled for the 22nd.
On 10/15/2019 @ 5:54 pm - I spent 2 hours trying everything in this thread.
The System.Text.Json is just not mature enough yet
I tried almost everything in this chain.
Also, I will say this, I'm using Azure functions to code my solution and the code ONLY breaks in the Visual Studio Azure functions testing harness.
I broke my code out in a .NetCore Console App and it worked perfectly using System.Text.Json.
So, it might be something in one of my many runtimes.
Thanks to the guys at the following blog, they helped me confirm my issue:
@ahsonkhan
@pranavkm
@markgr
https://github.com/aspnet/AspNetCore/issues/13093
@ahsonkhan - Hey man, thanks for the information... (Thanks for the information - I will go to the URL listed above)
@pranavkm - I used the following tested versions before posting to the blog. I didn't want to mis-lead people because I hadn't done my research
@markgr - Thanks for the commands - here's my dump
dotnet --list-sdks
2.1.602 [C:\Program Files\dotnet\sdk]
2.1.700 [C:\Program Files\dotnet\sdk]
2.1.801 [C:\Program Files\dotnet\sdk]
3.0.100 [C:\Program Files\dotnet\sdk]
3.1.100-preview1-014459 [C:\Program Files\dotnet\sdk]
dotnet --list-runtimes
Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\sharedMicrosoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\sharedMicrosoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\sharedMicrosoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\sharedMicrosoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\sharedMicrosoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.0-preview1.19508.20 [C:\Program Files\dotnet\sharedMicrosoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.0-preview1.19506.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.0-preview1.19506.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
You guys are great - thanks for helping me out by posting the information above - appreciate it!
Later
@dtate22 Are you suggesting you solved this problem? I was under the impression it was not solvable (pinned reference) and that all I could do was wait till (~10/25) for the new AFv3 to come out.
@electricessence 10/25 is the next comunity update. Last update on AzFunc core 3 estimated 2020 q1 was ga
I had this very same issue with my Blazor app.
Changing the TargetFramework of my Blazor client app and shared library projects from netstandard2.1 to netstandard2.0 resolved the issue.
Most helpful comment
I had this very same issue with my Blazor app.
Changing the
TargetFrameworkof my Blazor client app and shared library projects from netstandard2.1 to netstandard2.0 resolved the issue.