Para o código:
AppendBlobClient.AppendBlob(Stream)
Às vezes estou recebendo a exceção:
The value for one of the HTTP headers is not in the correct format.
RequestId:9d7754b2-4e3d-4256-97f8-f77b43ab3756
Time:2023-12-13T19:36:11.536Z
Status: 400 (The value for one of the HTTP headers is not in the correct format.)
ErrorCode: InvalidHeaderValue
Additional Information:
HeaderName: content-length
HeaderValue: 0
Content:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Error>
<Code>InvalidHeaderValue</Code>
<Message>The value for one of the HTTP headers is not in the correct format.
RequestId:9d7754b2-4e3d-4256-97f8-f77b43ab3756
Time:2023-12-13T19:36:11.536Z</Message>
<HeaderName>content-length</HeaderName>
<HeaderValue>0</HeaderValue>
</Error>
Headers:
Server: Azurite-Blob/3.26.0
x-ms-error-code: InvalidHeaderValue
x-ms-request-id: 9d7754b2-4e3d-4256-97f8-f77b43ab3756
Date: Wed, 13 Dec 2023 19:36:11 GMT
Connection: keep-alive
Keep-Alive: REDACTED
Transfer-Encoding: chunked
Content-Type: application/xml
Aula completa:
public class BlobQueueWriter : QueueWriterBase
{
private BlobServiceClient ServiceClient { get; }
private string ContainerName { get; }
private string BlobName { get; }
private BlobContainerClient Container { get; set; }
private AppendBlobClient AppendBlob { get; set; }
/// <summary>
/// Create the object. This will prepare to write to BLOB storage
/// </summary>
/// <param name="azureBlobConnectionString">The Azure connection string.</param>
/// <param name="path">The full pathname of the BLOB to write to. Includes the container.</param>
/// <param name="options">The options for this logger.</param>
public BlobQueueWriter(string? azureBlobConnectionString, string path, BlobLoggerOptions options) : base(options)
{
ServiceClient = new BlobServiceClient(azureBlobConnectionString);
(ContainerName, BlobName) = path.SplitBlobFilename();
Container = ServiceClient.GetBlobContainerClient(ContainerName);
AppendBlob = Container.GetAppendBlobClient(BlobName);
// the ILoggerProvider starts everything with a constructor call - so no async
Container.CreateIfNotExists();
// create the blob if it does not exist
if (!AppendBlob.Exists())
AppendBlob.CreateIfNotExists();
}
private static readonly byte[] CrLf = "\r\n"u8.ToArray();
/// <inheritdoc />
public override void WriteLine(List<string> messages)
{
try
{
using (var stream = new MemoryStream())
{
foreach (var message in messages)
{
var bytes = Encoding.UTF8.GetBytes(message);
stream.Write(bytes, 0, bytes.Length);
stream.Write(CrLf);
}
stream.Position = 0;
AppendBlob.AppendBlock(stream);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"BlobQueueWriter.WriteLine() threw exception {ex}");
Container = ServiceClient.GetBlobContainerClient(ContainerName);
AppendBlob = Container.GetAppendBlobClient(BlobName);
}
}
}
Alguma ideia do que há de errado?
Isso pode acontecer quando as mensagens estão vazias. Tente adicionar uma verificação e pule a invocação de AppendBlob.AppendBlock(stream); nessa condição.