MicrosoftLearning / AZ-204-DevelopingSolutionsforMicrosoftAzure

AZ-204: Developing solutions for Microsoft Azure
https://microsoftlearning.github.io/AZ-204-DevelopingSolutionsforMicrosoftAzure/
MIT License
2.29k stars 1.85k forks source link

Update AZ-204_lab_02.md Instruction and solution code to use Asynchronous I/O for functions #819

Closed Hortlund closed 2 months ago

Hortlund commented 2 months ago

Module/Lab: 02

Fixes Example and solution code giving runtime error

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

Changes proposed in this pull request: Instructions to use Asynchronous I/O for functions in example and solution code

Relevant Issues link

None

WigF1 commented 2 months ago

What issue is this attempting to resolve? The lab steps work as written and the code works as written.

Hortlund commented 2 months ago

@WigF1

I tried again running the example for Echo with the curl examples

curl -X POST -i http://localhost:7071/api/echo -d 3

Functions:

Echo: [GET,POST] http://localhost:7071/api/Echo

GetSettingInfo: [GET,POST] http://localhost:7071/api/GetSettingInfo

Recurring: timerTrigger

For detailed output, run func with --verbose flag. [2024-09-22T13:06:27.739Z] Host lock lease acquired by instance ID '0000000000000000000000004674353B'. [2024-09-22T13:06:47.760Z] Executing 'Functions.Echo' (Reason='This function was programmatically called via the host APIs.', Id=8a048387-d3d0-4554-8a4f-8acf8a8fb37f) [2024-09-22T13:06:47.878Z] C# HTTP trigger function processed a request. [2024-09-22T13:06:47.887Z] Function 'Echo', Invocation id '8a048387-d3d0-4554-8a4f-8acf8a8fb37f': An exception was thrown by the invocation. [2024-09-22T13:06:47.887Z] Result: Function 'Echo', Invocation id '8a048387-d3d0-4554-8a4f-8acf8a8fb37f': An exception was thrown by the invocation. Exception: System.InvalidOperationException: Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.

Kestrel doesnt allow synchronous calls by default.

using both SDKS 9.0.100-rc.1.24452.12 and 8.0.400

<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage" Version="6.6.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />

these are also all the packages i need to be able to run the project without issues as per the examples.

When adding async to the code it works.

This is when running the functions locally.

msftnutta commented 2 months ago

agreed with @WigF1 that currently the code and instruction for this lab working as per normal. Hence, i'll close this PR.

jv-k commented 3 days ago

I'm on MacOS with dotnet v8 and had this exact issue. I fixed it like this:

using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;

var builder = FunctionsApplication.CreateBuilder(args);

builder.Services.Configure<Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions>(options =>
{
    options.AllowSynchronousIO = true;
});

builder.ConfigureFunctionsWebApplication();

// Application Insights isn't enabled by default. See https://aka.ms/AAt8mw4.
// builder.Services
//     .AddApplicationInsightsTelemetryWorkerService()
//     .ConfigureFunctionsApplicationInsights();

builder.Build().Run();
Hortlund commented 3 days ago

@jv-k Yes that was an option too. My hopes was to not deviate from the default value and change to asynchronous methods instead since that is recommended.