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

.NET 7.0 Isolated Worker中BlobTrigger转Stream失败问题求助

.NET 7 Isolated模式BlobTrigger使用Stream崩溃问题排查

问题场景

在.NET 7.0的dotnet-isolated模式下,按照文档说明使用Stream处理Blob时,函数执行崩溃。

使用的NuGet包

<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs" Version="6.2.0" /> 
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" />

函数代码

[Function(nameof(CMImageResizer))]
public async Task Run(
    [BlobTrigger("rawimages/{name}", Connection = "StorageAccountConnectionString")] Stream myBlob,
    [BlobInput("resizedimages/{name}", Connection = "StorageAccountConnectionString")] Stream resizedImage, string name)
{
  try
   {
   _log.LogInformation("Processing image:{name} size: {myBlobLength} Bytes", name, myBlob.Length);

报错信息

Executing 'Functions.CMImageResizer' (Reason='New blob detected(LogsAndContainerScan): rawimages/taladro1.jpg', Id=00c22256-e65c-478c-82c3-eaca92c310b7) 
[2023-12-24T05:35:39.632Z] Trigger Details: MessageId: 2f450295-dae1-42f5-ac2e-089d5b0d2a55, DequeueCount: 1, InsertedOn: 2023-12-24T05:35:38.000+00:00, BlobCreated: 2023-12-24T04:42:41.000+00:00, BlobLastModified: 2023-12-24T05:35:28.000+00:00 
[2023-12-24T05:35:40.601Z] Function 'CMImageResizer', Invocation id '00c22256-e65c-478c-82c3-eaca92c310b7': An exception was thrown by the invocation. 
[2023-12-24T05:35:40.604Z] Result: Function 'CMImageResizer', Invocation id '00c22256-e65c-478c-82c3-eaca92c310b7': An exception was thrown by the invocation. 
[2023-12-24T05:35:40.604Z] Exception: Microsoft.Azure.Functions.Worker.FunctionInputConverterException: Error converting 1 input parameters for Function 'CMImageResizer': Cannot convert input parameter 'myBlob' to type 'System.IO.Stream' from type 'System.ReadOnlyMemory`1[[System.Byte, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]'. Error:System.Text.Json.JsonException: 'R' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
[2023-12-24T05:35:40.604Z] ---&gt; System.Text.Json.JsonReaderException: 'R' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0.
[2023-12-24T05:35:40.604Z] at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader&amp; json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes) 
[2023-12-24T05:35:40.604Z] at System.Text.Json.Utf8JsonReader.ConsumeValue(Byte marker) 
[2023-12-24T05:35:40.604Z] at System.Text.Json.Utf8JsonReader.ReadFirstToken(Byte first) 
[2023-12-24T05:35:40.604Z] at System.Text.Json.Utf8JsonReader.ReadSingleSegment() 
[2023-12-24T05:35:40.604Z] at System.Text.Json.Utf8JsonReader.Read() 
[2023-12-24T05:35:40.604Z] at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader&amp; reader, JsonSerializerOptions options, ReadStack&amp; state)

问题原因及修复方案

核心问题

  1. JSON转换器误处理二进制数据:.NET Isolated模式默认使用JSON转换器解析输入参数,Blob的二进制内容(如图片)被错误当作JSON解析,导致JsonReaderException。
  2. 绑定类型错误:代码中使用BlobInput绑定目标Blob,但BlobInput是用于读取现有Blob的输入绑定,而你需要写入处理后的图片,应使用BlobOutput绑定。

修复步骤

方案1:使用BinaryData处理输入(推荐)

将BlobTrigger参数改为BinaryData类型,再转换为Stream使用,同时修正输出绑定类型:

[Function(nameof(CMImageResizer))]
public async Task Run(
    [BlobTrigger("rawimages/{name}", Connection = "StorageAccountConnectionString")] BinaryData myBlobData,
    [BlobOutput("resizedimages/{name}", Connection = "StorageAccountConnectionString")] Stream resizedImage, 
    string name,
    ILogger log)
{
    try
    {
        using Stream myBlob = myBlobData.ToStream();
        log.LogInformation("Processing image:{name} size: {myBlobLength} Bytes", name, myBlob.Length);
        
        // 此处添加图片缩放逻辑,处理后写入resizedImage流
        // 示例:await myBlob.CopyToAsync(resizedImage);
    }
    catch (Exception ex)
    {
        log.LogError(ex, "Failed to process image {name}", name);
        throw;
    }
}

方案2:保留Stream类型并添加BinaryData属性

如果坚持使用Stream作为输入参数,需添加[BinaryData]属性告知运行时这是二进制数据:

[Function(nameof(CMImageResizer))]
public async Task Run(
    [BlobTrigger("rawimages/{name}", Connection = "StorageAccountConnectionString")] [BinaryData] Stream myBlob,
    [BlobOutput("resizedimages/{name}", Connection = "StorageAccountConnectionString")] Stream resizedImage, 
    string name,
    ILogger log)
{
    try
    {
        log.LogInformation("Processing image:{name} size: {myBlobLength} Bytes", name, myBlob.Length);
        // 后续处理逻辑
    }
    catch (Exception ex)
    {
        log.LogError(ex, "Failed to process image {name}", name);
        throw;
    }
}

注意事项

  • 确保Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs版本≥6.0.0,该版本开始支持Isolated模式下的二进制数据处理。
  • 使用BlobOutput绑定输出流时,无需手动关闭流,函数运行时会自动处理。

内容的提问来源于stack exchange,提问作者Vodo-Siosk Baas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 07:56:10