.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] ---> 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& 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& reader, JsonSerializerOptions options, ReadStack& state)
问题原因及修复方案
核心问题
- JSON转换器误处理二进制数据:.NET Isolated模式默认使用JSON转换器解析输入参数,Blob的二进制内容(如图片)被错误当作JSON解析,导致
JsonReaderException。 - 绑定类型错误:代码中使用
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
相关产品推荐
相关产品推荐

