Azure / azure-functions-vs-build-sdk

MSBuild task for Azure Functions
MIT License
96 stars 62 forks source link

DisposeAsync of Newtonsoft.Json.JsonTextReader throws exception "Entry point was not found" #610

Closed JeroenJSmit closed 1 year ago

JeroenJSmit commented 1 year ago

Summary

When upgrading a Function App (Net 6, Functions V4) to using the latests version of "Newtonsoft.Json" (13.0.3) the construction "await using" on a JsonTextReader instance throws an exception: Entry point was not found

Known workarounds

Repro In VS 2022 (17.5.1), create a new Function App (V4, .Net 6.0) with an Http trigger. Add the following package references:

As a smallest repro, use the following code in the HttpTrigger:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;

namespace FunctionApp.JsonTextReader
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            try
            {
                await Test1();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return new OkObjectResult("OK");
        }

        private static async Task Test1()
        {
            using var ms = new MemoryStream();
            using var sr = new StreamReader(ms, leaveOpen: true);
            await using var jsonReader = new Newtonsoft.Json.JsonTextReader(sr);
        }
    }
}

Call the trigger and the following exception is thrown and logged:

System.EntryPointNotFoundException: Entry point was not found.
   at System.IAsyncDisposable.DisposeAsync()
   at FunctionApp.JsonTextReader.Function1.Test1() in D:\TestProjects\FunctionApp.JsonTextReader\Function1.cs:line 37
   at FunctionApp.JsonTextReader.Function1.Run(HttpRequest req, ILogger log) in D:\TestProjects\FunctionApp.JsonTextReader\Function1.cs:line 23

Of course a void implementation but also when actually processing a stream with data this exception occurs. Note that the same construction works fine from unit tests, a console app and WebAPI.

Expected behavior No exception is thrown when the JsonTextReader is disposed.

bhagyshricompany commented 1 year ago

Hi @JeroenJSmit Thanks for update.Will check and confirm the same.

JeroenJSmit commented 1 year ago

Any news on this issue?

bhagyshricompany commented 1 year ago

@fabiocav pls comment and validate.

jviau commented 1 year ago

This is an in-proc function I am assuming? The functions host uses Newtonsoft.Json/13.0.2 at runtime, referencing 13.0.3 is not recommended as this version will not be loaded at runtime (13.0.2 will be loaded). Newtonsoft.Json only introduced IAsyncDIsposable in 13.0.3, which is why this fails at runtime.

If you wish to use 13.0.3, I suggest using dotnet isolated functions: https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide

ghost commented 1 year ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

bhagyshricompany commented 1 year ago

hope the soln provided work if not raise new req