Has anyone tried the following Microsoft quickstart “Connect Azure Functions to Azure Storage using Visual Studio Code” with Isolated worker model?
https://learn.microsoft.com/en-gb/azure/azure-functions/functions-add-output-binding-storage-queue-vs-code?pivots=programming-language-csharp&tabs=isolated-process
I have created an acync version and it works fine initially but then, after submitting 4 – 5 requests (by adding messages to the queue) got the following error and the function stops inserting messages to the queue:
[2024-12-08T05:54:01.909Z] Executed 'Functions.HttpTrigger3' (Failed, Id=d7c6cdb1-63a8-4c53-9e1f-abb7b6572ec4, Duration=70ms)
[2024-12-08T05:54:01.912Z] System.Private.CoreLib: Exception while executing function: Functions.HttpTrigger3. System.Private.CoreLib: Result:
Failure
Exception: System.ObjectDisposedException: IFeatureCollection has been disposed.
[2024-12-08T05:54:01.915Z] Object name: 'Collection'.
[2024-12-08T05:54:01.916Z] at Microsoft.AspNetCore.Http.Features.FeatureReferences`1.ThrowContextDisposed()
[2024-12-08T05:54:01.917Z] at Microsoft.AspNetCore.Http.Features.FeatureReferences`1.ContextDisposed()
[2024-12-08T05:54:01.918Z] at Microsoft.AspNetCore.Http.Features.FeatureReferences`1.Fetch[TFeature](TFeature& cached, Func`2 factory)
[2024-12-08T05:54:01.919Z] at Microsoft.AspNetCore.Http.DefaultHttpResponse.get_StatusCode()
[2024-12-08T05:54:01.920Z] at Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.AspNetCoreHttpResponseData.get_StatusCode() in /mnt/vss/_work/1/s/extensions/Worker.Extensions.Http.AspNetCore/src/HttpDataModel/AspNetCoreHttpResponseData.cs:line 39
[2024-12-08T05:54:01.921Z] at Microsoft.Azure.Functions.Worker.Rpc.RpcExtensions.ToRpcHttpAsync(HttpResponseData response, ObjectSerializer
serializer) in D:a_work1ssrcDotNetWorker.GrpcRpcExtensions.cs:line 88
[2024-12-08T05:54:01.922Z] at Microsoft.Azure.Functions.Worker.Rpc.RpcExtensions.ToRpcAsync(Object value, ObjectSerializer serializer) in D:a_work1ssrcDotNetWorker.GrpcRpcExtensions.cs:line 35
[2024-12-08T05:54:01.923Z] at Microsoft.Azure.Functions.Worker.Handlers.InvocationHandler.InvokeAsync(InvocationRequest request) in D:a_work1ssrcDotNetWorker.GrpcHandlersInvocationHandler.cs:line 102
Stack: at Microsoft.AspNetCore.Http.Features.FeatureReferences`1.ThrowContextDisposed()
[2024-12-08T05:54:01.924Z] at Microsoft.AspNetCore.Http.Features.FeatureReferences`1.ContextDisposed()
[2024-12-08T05:54:01.924Z] at Microsoft.AspNetCore.Http.Features.FeatureReferences`1.Fetch[TFeature](TFeature& cached, Func`2 factory)
[2024-12-08T05:54:01.925Z] at Microsoft.AspNetCore.Http.DefaultHttpResponse.get_StatusCode()
[2024-12-08T05:54:01.926Z] at Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.AspNetCoreHttpResponseData.get_StatusCode() in /mnt/vss/_work/1/s/extensions/Worker.Extensions.Http.AspNetCore/src/HttpDataModel/AspNetCoreHttpResponseData.cs:line 39
[2024-12-08T05:54:01.927Z] at Microsoft.Azure.Functions.Worker.Rpc.RpcExtensions.ToRpcHttpAsync(HttpResponseData response, ObjectSerializer serializer) in D:a_work1ssrcDotNetWorker.GrpcRpcExtensions.cs:line 88
[2024-12-08T05:54:01.928Z] at Microsoft.Azure.Functions.Worker.Rpc.RpcExtensions.ToRpcAsync(Object value, ObjectSerializer serializer) in D:a_work1ssrcDotNetWorker.GrpcRpcExtensions.cs:line 35
[2024-12-08T05:54:01.929Z] at Microsoft.Azure.Functions.Worker.Handlers.InvocationHandler.InvokeAsync(InvocationRequest request) in D:a_work1ssrcDotNetWorker.GrpcHandlersInvocationHandler.cs:line 102.
After a couple of minutes the function app recovers, but then after submitting several more requests got the error again.
Source code is listed below. I found the problem is related to the MultiResponse as if I change it to write to the queue only or just return HTTP response it works fine.
The In-process Model version works perfect when sending to the queue and returning HTTP response.
namespace HttpTriggerAppVSC
{
public class HttpExampleVSC
{
private readonly ILogger<HttpExampleVSC> _logger;
public HttpExampleVSC(ILogger<HttpExampleVSC> logger)
{
_logger = logger;
}
[Function("HttpTrigger3")]
public async Task<MultiResponse> HttpTrigger3(
[HttpTrigger(AuthorizationLevel.System, "get", "post")] HttpRequestData req,
FunctionContext executionContext, CancellationToken cancellationToken)
{
var logger = executionContext.GetLogger("HttpExampleVSC");
logger.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string message = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
//if (cancellationToken.IsCancellationRequested)
//{
// return null;
//}
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
await response.WriteStringAsync(message);
// Return a response to both HTTP trigger and storage output binding.
return new MultiResponse()
{
// Write a single message.
Messages = new string[] { message },
HttpResponse = response
};
}
}
public class MultiResponse
{
[QueueOutput("outqueue",Connection = "AzureStorageAccount")]
public string[] Messages { get; set; }
public HttpResponseData HttpResponse { get; set; }
}
}