最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

blobstorage - append blob with .NET appending data to separate arrays - Stack Overflow

programmeradmin0浏览0评论

I have a SubOrchestrator function that calls the following Activity Functions:

public async Task GetTransitiveMembers(IDurableOrchestrationContext context, GroupMembershipRequest request)
{
    if (!context.IsReplaying) _ = _log.LogMessageAsync(new LogMessage { RunId = request.RunId, Message = $"Getting results from 1st page" });
    var nextPageUrl = await context.CallActivityAsync<string>(nameof(MembersReaderFunction), new MembersReaderRequest { RunId = request.RunId, GroupId = request.SourceGroup.ObjectId, TargetGroupId = request.GroupId, CurrentPart = request.CurrentPart });


    while (!string.IsNullOrEmpty(nextPageUrl))
    {
        if (!context.IsReplaying) _ = _log.LogMessageAsync(new LogMessage { RunId = request.RunId, Message = $"Getting results from next page url: {nextPageUrl}" });
        var oldNextPageUrl = nextPageUrl;              
        nextPageUrl = await context.CallActivityAsync<string>(nameof(SubsequentMembersReaderFunction), new SubsequentMembersReaderRequest { RunId = request.RunId, NextPageUrl = nextPageUrl, GroupId = request.SourceGroup.ObjectId, TargetGroupId = request.GroupId, CurrentPart = request.CurrentPart })
    }
}

Activity functions:

[FunctionName(nameof(MembersReaderFunction))]
public async Task<string> GetMembersAsync([ActivityTrigger] MembersReaderRequest request)
{
    await _log.LogMessageAsync(new LogMessage { Message = $"{nameof(MembersReaderFunction)} function started", RunId = request.RunId }, VerbosityLevel.DEBUG);
    _calculator.RunId = request.RunId;
    var response = await _calculator.GetFirstTransitiveMembersPageAsync(request.GroupId, request.RunId);
    response.Users = new List<AzureADUser> { new AzureADUser { UserPrincipalName = "a" } };
    var fileName = $"/{request.TargetGroupId}/{request.RunId}_GroupMembership_{request.CurrentPart}.json";
    using (MemoryStream logEntryStream = new MemoryStream())
    {
        await JsonSerializer.SerializeAsync(logEntryStream, response.Users);
        logEntryStream.Position = 0;
        await _blobStorageRepository.AppendDataToBlobAsync(logEntryStream, fileName);
    }
    await _log.LogMessageAsync(new LogMessage { Message = $"{nameof(MembersReaderFunction)} function completed", RunId = request.RunId }, VerbosityLevel.DEBUG);
    return response.NextPageUrl;
}

[FunctionName(nameof(SubsequentMembersReaderFunction))]
public async Task<string> GetMembersAsync([ActivityTrigger] SubsequentMembersReaderRequest request)
{
    await _log.LogMessageAsync(new LogMessage { Message = $"{nameof(SubsequentMembersReaderFunction)} function started", RunId = request.RunId }, VerbosityLevel.DEBUG);
    _calculator.RunId = request.RunId;
    var response = await _calculator.GetNextTransitiveMembersPageAsync(request.NextPageUrl);
    response.Users = new List<AzureADUser> { new AzureADUser { UserPrincipalName = "b" } };
    var fileName = $"/{request.TargetGroupId}/{request.RunId}_GroupMembership_{request.CurrentPart}.json";
    using (MemoryStream logEntryStream = new MemoryStream())
    {
        await JsonSerializer.SerializeAsync(logEntryStream, response.Users);
        logEntryStream.Position = 0;
        await _blobStorageRepository.AppendDataToBlobAsync(logEntryStream, fileName);
    }
    await _log.LogMessageAsync(new LogMessage { Message = $"{nameof(SubsequentMembersReaderFunction)} function completed", RunId = request.RunId }, VerbosityLevel.DEBUG);
    return response.NextPageUrl;
}

BlobStorageRepository:

public async Task AppendDataToBlobAsync (MemoryStream logEntryStream, string logBlobName)
{
    var appendBlobClient = _containerClient.GetAppendBlobClient(logBlobName);

    await appendBlobClient.CreateIfNotExistsAsync();
    logEntryStream.Position = 0;

    var maxBlockSize = appendBlobClient.AppendBlobMaxAppendBlockBytes;
    var bytesLeft = logEntryStream.Length;
    var buffer = new byte[maxBlockSize];
    while (bytesLeft > 0)
    {
        var blockSize = (int)Math.Min(bytesLeft, maxBlockSize);
        var bytesRead = await logEntryStream.ReadAsync(buffer.AsMemory(0, blockSize));
        await using (MemoryStream memoryStream = new MemoryStream(buffer, 0, bytesRead))
        {
            await appendBlobClient.AppendBlockAsync(memoryStream);
        }
        bytesLeft -= bytesRead;
    }
}

I see the content in blob like this:

[
    {
        "ObjectId": "00000000-0000-0000-0000-000000000000",
        "Mail": null,
        "UserPrincipalName": "a",
        "Properties": null,
        "MembershipAction": null,
        "SourceGroup": "00000000-0000-0000-0000-000000000000",
        "SourceGroups": null
    }
][
    {
        "ObjectId": "00000000-0000-0000-0000-000000000000",
        "Mail": null,
        "UserPrincipalName": "b",
        "Properties": null,
        "MembershipAction": null,
        "SourceGroup": "00000000-0000-0000-0000-000000000000",
        "SourceGroups": null
    }
][
    {
        "ObjectId": "00000000-0000-0000-0000-000000000000",
        "Mail": null,
        "UserPrincipalName": "b",
        "Properties": null,
        "MembershipAction": null,
        "SourceGroup": "00000000-0000-0000-0000-000000000000",
        "SourceGroups": null
    }
]

It should be:

[
    {
        "ObjectId": "00000000-0000-0000-0000-000000000000",
        "Mail": null,
        "UserPrincipalName": "a",
        "Properties": null,
        "MembershipAction": null,
        "SourceGroup": "00000000-0000-0000-0000-000000000000",
        "SourceGroups": null
    },
    {
        "ObjectId": "00000000-0000-0000-0000-000000000000",
        "Mail": null,
        "UserPrincipalName": "b",
        "Properties": null,
        "MembershipAction": null,
        "SourceGroup": "00000000-0000-0000-0000-000000000000",
        "SourceGroups": null
    },
    {
        "ObjectId": "00000000-0000-0000-0000-000000000000",
        "Mail": null,
        "UserPrincipalName": "b",
        "Properties": null,
        "MembershipAction": null,
        "SourceGroup": "00000000-0000-0000-0000-000000000000",
        "SourceGroups": null
    }
] How do I fix this?
发布评论

评论列表(0)

  1. 暂无评论