In Visual Studio I created a new Azure Function project with an HttpTrigger function. This is the code for the function:
public class DoSomething
{
private readonly ILogger<DoSomething> _logger;
public DoSomething(ILogger<DoSomething> logger)
{
_logger = logger;
}
[Function("DoSomething")]
public async Task Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
{
try
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
for (int i = 0; i<7; i++)
{
_logger.LogInformation($"Working... {i}");
await Task.Delay(1000);
}
_logger.LogInformation("Done");
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred.");
}
_logger.LogInformation("After Exception");
return;
}
}
The function is called with this console application:
Task.Run(async () =>
{
using var client = new HttpClient();
var res = await client.PostAsync("http://localhost:7129/api/DoSomething", null);
});
//Do other stuff
await Task.Delay(4000);
Intentionally my console application closes before the function completes its work. In the function log I get these messages:
[2025-02-06T10:38:32.483Z] C# HTTP trigger function processed a request.
[2025-02-06T10:38:32.485Z] Working... 0
[2025-02-06T10:38:33.486Z] Working... 1
[2025-02-06T10:38:34.491Z] Working... 2
[2025-02-06T10:38:35.497Z] Working... 3
[2025-02-06T10:38:36.508Z] Working... 4
[2025-02-06T10:38:37.518Z] Working... 5
[2025-02-06T10:38:38.533Z] Working... 6
[2025-02-06T10:38:39.549Z] Done
[2025-02-06T10:38:39.552Z] After Exception
[2025-02-06T10:38:39.593Z] Function 'DoSomething', Invocation id '5a240a47-710b-482e-8d43-3a0fdf08c73b': An exception was thrown by the invocation.
[2025-02-06T10:38:39.596Z] Result: Function 'DoSomething', Invocation id '5a240a47-710b-482e-8d43-3a0fdf08c73b': An exception was thrown by the invocation.
Exception: System.InvalidOperationException: An attempt was made to transition a task to a final state when it had already completed.
[2025-02-06T10:38:39.599Z] at System.Threading.Tasks.TaskCompletionSource`1.SetCanceled(CancellationToken cancellationToken)
[2025-02-06T10:38:39.601Z] at System.Threading.Tasks.TaskCompletionSource`1.SetCanceled()
[2025-02-06T10:38:39.603Z] at Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.ContextReference.CompleteFunction() in D:\a\_work\1\s\extensions\Worker.Extensions.Http.AspNetCore\src\Coordinator\ContextReference.cs:line 59
[2025-02-06T10:38:39.606Z] at Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.DefaultHttpCoordinator.CompleteFunctionInvocation(String invocationId) in D:\a\_work\1\s\extensions\Worker.Extensions.Http.AspNetCore\src\Coordinator\DefaultHttpCoordinator.cs:line 55
[2025-02-06T10:38:39.608Z] at Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.FunctionsHttpProxyingMiddleware.Invoke(FunctionContext context, FunctionExecutionDelegate next) in D:\a\_work\1\s\extensions\Worker.Extensions.Http.AspNetCore\src\FunctionsMiddleware\FunctionsHttpProxyingMiddleware.cs:line 69
[2025-02-06T10:38:39.612Z] at Microsoft.Azure.Functions.Worker.FunctionsApplication.InvokeFunctionAsync(FunctionContext context) in D:\a\_work\1\s\src\DotNetWorker.Core\FunctionsApplication.cs:line 77
Stack: at System.Threading.Tasks.TaskCompletionSource`1.SetCanceled(CancellationToken cancellationToken)
[2025-02-06T10:38:39.614Z] at System.Threading.Tasks.TaskCompletionSource`1.SetCanceled()
[2025-02-06T10:38:39.616Z] at Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.ContextReference.CompleteFunction() in D:\a\_work\1\s\extensions\Worker.Extensions.Http.AspNetCore\src\Coordinator\ContextReference.cs:line 59
[2025-02-06T10:38:39.618Z] at Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.DefaultHttpCoordinator.CompleteFunctionInvocation(String invocationId) in D:\a\_work\1\s\extensions\Worker.Extensions.Http.AspNetCore\src\Coordinator\DefaultHttpCoordinator.cs:line 55
[2025-02-06T10:38:39.621Z] at Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.FunctionsHttpProxyingMiddleware.Invoke(FunctionContext context, FunctionExecutionDelegate next) in D:\a\_work\1\s\extensions\Worker.Extensions.Http.AspNetCore\src\FunctionsMiddleware\FunctionsHttpProxyingMiddleware.cs:line 69
[2025-02-06T10:38:39.624Z] at Microsoft.Azure.Functions.Worker.FunctionsApplication.InvokeFunctionAsync(FunctionContext context) in D:\a\_work\1\s\src\DotNetWorker.Core\FunctionsApplication.cs:line 77.
[2025-02-06T10:38:39.678Z] Executed 'Functions.DoSomething' (Failed, Id=5a240a47-710b-482e-8d43-3a0fdf08c73b, Duration=7506ms)
[2025-02-06T10:38:39.680Z] System.Private.CoreLib: Exception while executing function: Functions.DoSomething. Microsoft.Azure.WebJobs.Script.Grpc: Failed to proxy request with ForwarderError: RequestCanceled. System.Net.Http: The operation was canceled. System.Net.Sockets: Unable to read data from the transport connection: Operazione di I/O terminata a causa dell'uscita dal thread oppure della richiesta di un'applicazione.. Operazione di I/O terminata a causa dell'uscita dal thread oppure della richiesta di un'applicazione.
Why does my function code throw an exception?
The console application closes during the “working... X” phase and if there was an exception I was expecting it during the execution of the function code.
Is there any way to avoid the exception without having to wait for the function response from the caller?
Thank you


awaitin your console code i.e.,await Task.Run(async () =>