You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Functions独立工作者模型写入存储队列遇ObjectDisposedException错误

Azure Functions独立工作者模型多响应输出绑定报错问题

有人尝试过微软官方快速入门《使用Visual Studio Code将Azure Functions连接到Azure存储》的独立工作者模型(Isolated Worker Model)版本吗?

我实现了异步版本,初始运行正常,但提交4-5个请求(向队列添加消息)后出现以下错误,函数停止向队列插入消息:

[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\_work\1\s\src\DotNetWorker.Grpc\RpcExtensions.cs:line 88
[2024-12-08T05:54:01.922Z]    at Microsoft.Azure.Functions.Worker.Rpc.RpcExtensions.ToRpcAsync(Object value, ObjectSerializer serializer) in D:\a\_work\1\s\src\DotNetWorker.Grpc\RpcExtensions.cs:line 35
[2024-12-08T05:54:01.923Z]    at Microsoft.Azure.Functions.Worker.Handlers.InvocationHandler.InvokeAsync(InvocationRequest request) in D:\a\_work\1\s\src\DotNetWorker.Grpc\Handlers\InvocationHandler.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\_work\1\s\src\DotNetWorker.Grpc\RpcExtensions.cs:line 88
[2024-12-08T05:54:01.928Z]    at Microsoft.Azure.Functions.Worker.Rpc.RpcExtensions.ToRpcAsync(Object value, ObjectSerializer serializer) in D:\a\_work\1\s\src\DotNetWorker.Grpc\RpcExtensions.cs:line 35
[2024-12-08T05:54:01.929Z]    at Microsoft.Azure.Functions.Worker.Handlers.InvocationHandler.InvokeAsync(InvocationRequest request) in D:\a\_work\1\s\src\DotNetWorker.Grpc\Handlers\InvocationHandler.cs:line 102.

几分钟后函数应用会恢复,但再次提交多个请求后会再次报错。

我发现问题与MultiResponse有关:若仅写入队列或仅返回HTTP响应则运行正常;而进程内模型(In-process Model)版本在同时发送队列消息和返回HTTP响应时运行完美。

以下是源代码:

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.";
    
            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; }
    } 
}

内容的提问来源于stack exchange,提问作者Angel Komarov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 15:57:32