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 @ahsonkhan @steveharter @GrabYourPitchforks
Any workarounds? I would hate to have to revert the updates that are currently using System.Text.Json in other libraries. That's another strange fruit, this error is occurring when running an Azure Function but System.Text.Json was attached to another dependency and not directly imported.
I'm not quite sure how assembly resolution works in Core, so somebody more knowledgeable would need to chime in. But it sounds very much like an RTM package trying to run on a Preview framework (or vice versa).
:(
I pinged some folks more knowledgeable about assembly packaging / deployment / loading. Hopefully they'll be able to respond after the weekend. :)
Azure functions pins versions of specific assemblies so you could be running on an older version of cetain bits. Can you share your functions project dependencies?
I haven't used Azure Functions a lot so it will take some time for me to give it a try and figure out the root cause. If you have an easy way to repro the issue or can provide more info about your dependencies that are getting imported and the call-stack that's resulting in the missing method error, that would be helpful. Also, if you have a project that you are deploying to Azure Functions, can you share what it looks like (i.e. what's your target framework version/ package dependencies - maybe share your csproj)?
At a quick glance, it looks like some dependency is bringing in an older/out-dated version of System.Text.Encodings.Web and that is conflicting with the newer version that System.Text.Json depends on (one that contains the newly added API). The AF host might be relying on an older version of S.T.E.Web and loading that first causing the mismatch (and as @davidfowl said, this is likely due to versions being pinned). If the problem isn't because of how the AF host itself loads assemblies, then explicitly adding a package reference to the latest S.T.E.Web NuGet package (and/or S.T.Json) might end up working around the issue.
cc @fabiocav
Looks similar to https://github.com/Azure/azure-sdk-for-net/issues/7354. As @davidfowl mentioned, Azure functions act as a host and pin parts of the framework, not allowing functions to bring newer versions. This is a bug in Azure functions and should probably be moved to https://github.com/Azure/azure-functions-host.
Yep, 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):


I couldn't move the issue there, so opened a new issue in azure functions: https://github.com/Azure/azure-functions-host/issues/5031
Closing this one.
@ahsonkhan Thank you.
Most helpful comment
Looks similar to https://github.com/Azure/azure-sdk-for-net/issues/7354. As @davidfowl mentioned, Azure functions act as a host and pin parts of the framework, not allowing functions to bring newer versions. This is a bug in Azure functions and should probably be moved to https://github.com/Azure/azure-functions-host.