[FunctionName(nameof(MembersReaderFunction ))]
public async Task<string> GetMembersAsync([ActivityTrigger] MembersReaderRequest request)
{
var response = await getFirstMembersAsync(request.NextPageUrl);
response.Users = new List<AzureADUser> { new AzureADUser { UserPrincipalName = "a" } };
var fileName = "a.json";
using (MemoryStream logEntryStream = new MemoryStream())
{
await JsonSerializer.SerializeAsync(logEntryStream, users);
logEntryStream.Position = 0;
await AppendDataToBlobAsync(logEntryStream, fileName);
}
return response.NextPageUrl;
}
[FunctionName(nameof(SubsequentMembersReaderFunction))]
public async Task<string> GetMembersAsync([ActivityTrigger] SubsequentMembersReaderRequest request)
{
var response = await getNextMembersAsync(request.NextPageUrl);
response.Users = new List<AzureADUser> { new AzureADUser { UserPrincipalName = "b" } };
var fileName = "a.json";
using (MemoryStream logEntryStream = new MemoryStream())
{
await JsonSerializer.SerializeAsync(logEntryStream, response.Users);
logEntryStream.Position = 0;
await AppendDataToBlobAsync(logEntryStream, fileName);
}
return response.NextPageUrl;
}
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:
[
{
"UserPrincipalName": "a"
}
][
{
"UserPrincipalName": "b"
}
][
{
"UserPrincipalName": "b"
}
]
It should be:
[
{
"UserPrincipalName": "a"
},
{
"UserPrincipalName": "b"
},
{
"UserPrincipalName": "b"
}
]
How do I fix this?
.txt
{
"UserPrincipalName": "a"
}
{
"UserPrincipalName": "b"
}
{
"UserPrincipalName": "c"
}
List<AzureADUser>, so you end up with a bunch of lists joined together. (each list happens to have 1 object in it, in your example.) The solutions would be either read the entire list from blob each time and then append and write back; or else craft the JSON via an algorithm of your own invention rather than using the normal serialiser. I guess I could try and write an answer for the 1st option if that's useful?