Azure-functions-host: Using latest .NET Core 3.0 packages with Azure Functions v2 (like System.Text.Encodings.Web) causes "Method not found" issue due to assembly version mismatch

Created on 7 Oct 2019  路  16Comments  路  Source: Azure/azure-functions-host

Is your question related to a specific version? If so, please specify:

Azure Functions v2

What language does your question apply to? (e.g. C#, JavaScript, Java, All)

C#

Question

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


netcoreapp2.1
v2







PreserveNewest


PreserveNewest
Never


```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:
image

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

image

Most helpful comment

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.

All 16 comments

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.

  • I tried the simplest solutions including just switching my versions of Microsoft.NetCore
  • I also attempted to use different methods to parse
  • Because I'm on a timeline, I had to drop back and punt because my code has to be production ready and I cannot afford to take too many chances and break my release cycle

The System.Text.Json is just not mature enough yet

  • I had to drop back from
    OpportunityChangesLastXMins oBody = JsonSerializer.Deserialize(sJSON);
  • To:
    OpportunityChangesLastXMins oBody = JsonConvert.DeserializeObject(sJSON);

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)

  • It's crazy how many different places you have to look to solve a problem.

@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

  • .netCore 2.0 - failed (nuget System.Text.Json version)
  • .netCore 2.1 - failed (nuget System.Text.Json version)
  • .netCore 2.2 - failed (nuget System.Text.Json version)
  • .netCore 3.0 - failed (for this version, I had to uninstall the packages because the JSON is native)
  • .netCore 3.1 - failed - would not compile my Azure Function

@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.

Was this page helpful?
0 / 5 - 0 ratings