Azure function works as expected locally but not always when deployed to Azure

Griet Van Compernolle 0 Reputation points
2025-12-18T06:25:36.9933333+00:00

I created an Azure Function to convert a flat file to XML by making use of EDIFabric (licensed). When I execute the function locally, the function always works as expected.

But when the function is deployed to Azure, only the first time the function gives me the correct output. The other attempts I don't get the expected output, but I don't receive an exception either.

AppInsight is activated, but I don't find any clarification for this behaviour.

What could be the reason for this behaviour?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Siddhesh Desai 815 Reputation points Microsoft External Staff Moderator
    2025-12-25T06:22:55.9733333+00:00

    Hi @Griet Van Compernolle

    Thank you for reaching out to Microsoft Q&A

    This behavior is typically caused by shared mutable state being reused across Azure Function invocations. Reusing an EdiReader,X12Reader,EdifactReader instance across requests aren’t documented as thread‑safe.

    1. Reusing a Stream (e.g., a Memory Stream) and forgetting to reset Position to 0 or appending to the same static stream—first run looks fine, later runs read wrong content.
    2. Mutating global EDIFabric configuration (e.g., separators, ReaderSettings, template assembly name) in static fields during one invocation that then affects the next.

    To resolve this issue:

    Create streams and readers per invocation and dispose them immediately:

    [Function("ConvertEdi")]
    public async Task<HttpResponseData> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req)
    {
        var input = await new StreamReader(req.Body).ReadToEndAsync();
        using var stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
        using var reader = new X12Reader(stream, "EdiFabric.Templates.X12");
        var items = reader.ReadToEnd();
        var xml = string.Join("\n", items.Select(i => i.Serialize().ToString()));
        var res = req.CreateResponse();
        await res.WriteStringAsync(xml);
        return res;
    }
    

    Enable Logging to find out the cause:

    Log a hash of the output per invocation

    Adjust your Program.cs to give Information Logs.

    _log.LogInformation("Output hash: {hash}", SHA256.HashData(Encoding.UTF8.GetBytes(xml)));
    

    Correlate hashes across calls in Application Insights and find why the invocation is not happening.

    You can monitor function logs by running KQL query: https://github.com/MicrosoftDocs/azure-docs/blob/main/articles/azure-functions/monitor-functions.md and get more insights.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.