Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visual Studio: New Durable Functions Project: System.InvalidOperationException: Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead. #2425

Closed
jaliyaudagedara opened this issue Apr 22, 2024 · 2 comments
Labels
potential-bug Items opened using the bug report template, not yet triaged and confirmed as a bug

Comments

@jaliyaudagedara
Copy link

jaliyaudagedara commented Apr 22, 2024

Description

When creating a new Azure Function App Project and selecting Durable Functions Orchestration, the basic functionality is erroring out when the default Function1_HttpStart is triggered.

Error: System.InvalidOperationException: Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead.

Azure Functions Core Tools
Core Tools Version:       4.0.5611 Commit hash: N/A +591b8aec842e333a87ea9e23ba390bb5effe0655 (64-bit)
Function Runtime Version: 4.31.1.22191

[2024-04-22T09:04:53.962Z] Found C:\Users\Jaliya\Desktop\FunctionApp1\FunctionApp1\FunctionApp1.csproj. Using for user secrets file configuration.
[2024-04-22T09:04:58.877Z] Azure Functions .NET Worker (PID: 46364) initialized in debug mode. Waiting for debugger to attach...
[2024-04-22T09:04:58.941Z] Worker process started and initialized.

Functions:

        Function1_HttpStart: [GET,POST] http://localhost:7036/api/Function1_HttpStart

        Function1: orchestrationTrigger

        SayHello: activityTrigger

For detailed output, run func with --verbose flag.
[2024-04-22T09:05:03.912Z] Host lock lease acquired by instance ID '000000000000000000000000CF6A328F'.
[2024-04-22T09:05:15.399Z] Executing 'Functions.Function1_HttpStart' (Reason='This function was programmatically called via the host APIs.', Id=867fd6a5-d97b-430a-aef5-aa1c66621d8a)
[2024-04-22T09:05:15.661Z] Scheduling new Function1 orchestration with instance ID '41abf12af72249beab7a62f92bfbdc12' and 0 bytes of input data.
[2024-04-22T09:05:15.838Z] Started orchestration with ID = '41abf12af72249beab7a62f92bfbdc12'.
[2024-04-22T09:05:15.893Z] Executing 'Functions.Function1' (Reason='(null)', Id=a9158030-742b-4336-8488-0bddcfaddc25)
[2024-04-22T09:05:16.038Z] Function 'Function1_HttpStart', Invocation id '867fd6a5-d97b-430a-aef5-aa1c66621d8a': An exception was thrown by the invocation.
[2024-04-22T09:05:16.039Z] Result: Function 'Function1_HttpStart', Invocation id '867fd6a5-d97b-430a-aef5-aa1c66621d8a': An exception was thrown by the invocation.
Exception: System.InvalidOperationException: Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead.
[2024-04-22T09:05:16.040Z]    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 count)

Visual Studio Version:

Microsoft Visual Studio Enterprise 2022 (64-bit) - Preview
Version 17.10.0 Preview 4.0

Steps to reproduce

  1. Create a new Azure Function App Project and select Durable Functions Orchestration
  2. Execute the Function1_HttpStart endpoint
    image
@jaliyaudagedara jaliyaudagedara added the potential-bug Items opened using the bug report template, not yet triaged and confirmed as a bug label Apr 22, 2024
@jaliyaudagedara
Copy link
Author

jaliyaudagedara commented Apr 22, 2024

This was a known issue for some time, and I saw the template was fixed on this PR (Azure/azure-functions-templates#1513).

Related issues:

But somehow VS isn't using the updated templates.

The code generated by VS,

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.0.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
  </ItemGroup>
</Project>
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.DurableTask;
using Microsoft.DurableTask.Client;
using Microsoft.Extensions.Logging;

namespace FunctionApp1
{
    public static class Function1
    {
        [Function(nameof(Function1))]
        public static async Task<List<string>> RunOrchestrator(
            [OrchestrationTrigger] TaskOrchestrationContext context)
        {
            ILogger logger = context.CreateReplaySafeLogger(nameof(Function1));
            logger.LogInformation("Saying hello.");
            var outputs = new List<string>();

            // Replace name and input with values relevant for your Durable Functions Activity
            outputs.Add(await context.CallActivityAsync<string>(nameof(SayHello), "Tokyo"));
            outputs.Add(await context.CallActivityAsync<string>(nameof(SayHello), "Seattle"));
            outputs.Add(await context.CallActivityAsync<string>(nameof(SayHello), "London"));

            // returns ["Hello Tokyo!", "Hello Seattle!", "Hello London!"]
            return outputs;
        }

        [Function(nameof(SayHello))]
        public static string SayHello([ActivityTrigger] string name, FunctionContext executionContext)
        {
            ILogger logger = executionContext.GetLogger("SayHello");
            logger.LogInformation("Saying hello to {name}.", name);
            return $"Hello {name}!";
        }

        [Function("Function1_HttpStart")]
        public static async Task<HttpResponseData> HttpStart(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
            [DurableClient] DurableTaskClient client,
            FunctionContext executionContext)
        {
            ILogger logger = executionContext.GetLogger("Function1_HttpStart");

            // Function input comes from the request content.
            string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(
                nameof(Function1));

            logger.LogInformation("Started orchestration with ID = '{instanceId}'.", instanceId);

            // Returns an HTTP 202 response with an instance management payload.
            // See https://learn.microsoft.com/azure/azure-functions/durable/durable-functions-http-api#start-orchestration
            return client.CreateCheckStatusResponse(req, instanceId);
        }
    }
}

@jviau
Copy link
Contributor

jviau commented Apr 24, 2024

The templates have been updated: Azure/azure-functions-templates#1513

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
potential-bug Items opened using the bug report template, not yet triaged and confirmed as a bug
Projects
None yet
Development

No branches or pull requests

2 participants