Problema de fundo:
Tenho um documento POCO que gostaria de usar com a CosmosDbOutput
extensão de vinculação (mais recente, v4).
Prefiro ter meus nomes de propriedade POCO em PascaLCase, e meus documentos Cosmos/Json em camelCase respectivamente. No entanto, não consigo encontrar a maneira correta de modificar as opções de serialização para fazer isso funcionar.
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using System.Net;
using FromBodyAttribute = Microsoft.Azure.Functions.Worker.Http.FromBodyAttribute;
namespace FunctionCrudSample;
// Function
[Function(nameof(CreatePascalCase))]
public async Task<CreateResponsePascalCase> CreatePascalCase(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req,
[FromBody] MyDocumentPascalCase myDocument,
FunctionContext executionContext
)
{
var logger = executionContext.GetLogger(nameof(Api));
logger.LogInformation("C# HTTP trigger function processed a request.");
var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteAsJsonAsync(myDocument);
return new() { Response = response, Document = myDocument };
}
// Multi-Return type
public class CreateResponsePascalCase
{
[HttpResult]
public required HttpResponseData Response { get; set; }
[CosmosDBOutput(cosmosDbName, cosmosContainerName, Connection = connectionString, CreateIfNotExists = true, PartitionKey = "/id")]
public MyDocumentPascalCase? Document { get; set; }
}
// POCO
public class MyDocumentPascalCase
{
public string? Id { get; set; }
public string? Message { get; set; }
}
O que eu tentei
1. Modifique o POCO para usar nomes de propriedade camelCase
Isso funciona, mas, novamente. Eu preferiria usar PascalCase. Sem mencionar que usar nomes de propriedade em letras minúsculas viola o estilo de código IDE1006.
public class MyDocumentCamelCase
{
public string? id { get; set; }
public string? message { get; set; }
}
2. Configurando IOptions<WorkerOptions>
eConfigureCosmosDBExtensionOptions()
Não obtive sucesso com nenhuma ou ambas as opções de configuração noProgram.cs
Sem mencionar que eles não são documentados. Passei algumas horas no repositório dotnet worker runtime.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
var host = new HostBuilder().ConfigureFunctionsWorkerDefaults(options =>
{
options.Services.AddOptions<CosmosDBExtensionOptions>().Configure<IOptions<WorkerOptions>>((cosmos, worker) =>
{
cosmos.ClientOptions.SerializerOptions.PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase;
});
options.ConfigureCosmosDBExtensionOptions(options =>
{
options.ClientOptions.SerializerOptions.PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase;
});
}).Build();
await host.RunAsync();
Detalhes do erro
[2025-01-06T00:12:14.733Z] Executing 'Functions.CreatePascalCase' (Reason='This function was programmatically called via the host APIs.', Id=e3502c80-8f5d-45f8-8060-3f79526d959a)
[2025-01-06T00:12:14.835Z] C# HTTP trigger function processed a request.
[2025-01-06T00:12:15.542Z] Host lock lease acquired by instance ID '000000000000000000000000451B1ABA'.
[2025-01-06T00:12:22.899Z] Executed 'Functions.CreatePascalCase' (Failed, Id=e3502c80-8f5d-45f8-8060-3f79526d959a, Duration=8170ms)
[2025-01-06T00:12:22.900Z] System.Private.CoreLib: Exception while executing function: Functions.CreatePascalCase. Microsoft.Azure.Cosmos.Client: Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: 8ba46106-33ab-444c-9706-234f1c626bd4; Reason: ({"code":"BadRequest","message":"Message: {\"Errors\":[\"One of the specified inputs is invalid\"]}\r\nActivityId: 8ba46106-33ab-444c-9706-234f1c626bd4, Request URI: /apps/DocDbApp/services/DocDbServer1/partitions/a4cb494d-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, RequestStats: \r\nRequestStartTime: 2025-01-06T00:12:22.8309413Z, RequestEndTime: 2025-01-06T00:12:22.8326386Z, Number of regions attempted:1\r\n{\"systemHistory\":[{\"dateUtc\":\"2025-01-06T00:11:28.7225106Z\",\"cpu\":0.400,\"memory\":7185016.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.1436,\"availableThreads\":32766,\"minThreads\":16,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":1},{\"dateUtc\":\"2025-01-06T00:11:38.7228784Z\",\"cpu\":0.303,\"memory\":7172964.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.1101,\"availableThreads\":32766,\"minThreads\":16,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":1},{\"dateUtc\":\"2025-01-06T00:11:48.7368240Z\",\"cpu\":0.361,\"memory\":7234704.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0365,\"availableThreads\":32766,\"minThreads\":16,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":1},{\"dateUtc\":\"2025-01-06T00:11:58.7376400Z\",\"cpu\":0.557,\"memory\":7209968.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.1008,\"availableThreads\":32766,\"minThreads\":16,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":1},{\"dateUtc\":\"2025-01-06T00:12:08.7520453Z\",\"cpu\":4.037,\"memory\":6609924.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0644,\"availableThreads\":32766,\"minThreads\":16,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":1},{\"dateUtc\":\"2025-01-06T00:12:18.7587838Z\",\"cpu\":3.106,\"memory\":6173820.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0562,\"availableThreads\":32765,\"minThreads\":16,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":1}]}\r\nRequestStart: 2025-01-06T00:12:22.8311613Z; ResponseTime: 2025-01-06T00:12:22.8326386Z; StoreResult: StorePhysicalAddress: rntbd://127.0.0.1:10253/apps/DocDbApp/services/DocDbServer1/partitions/a4cb494d-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, LSN: 30, GlobalCommittedLsn: -1, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 400, SubStatusCode: 0, RequestCharge: 0, ItemLSN: -1, SessionToken: -1#30, UsingLocalLSN: False, TransportException: null, BELatencyMs: 0.297, ActivityId: 8ba46106-33ab-444c-9706-234f1c626bd4, RetryAfterInMs: , ReplicaHealthStatuses: [(port: 10253 | status: Connected | lkt: 1/4/2025 6:59:48 PM)], TransportRequestTimeline: {\"requestTimeline\":[{\"event\": \"Created\", \"startTimeUtc\": \"2025-01-06T00:12:22.8311628Z\", \"durationInMs\": 0.0208},{\"event\": \"ChannelAcquisitionStarted\", \"startTimeUtc\": \"2025-01-06T00:12:22.8311836Z\", \"durationInMs\": 0.0031},{\"event\": \"Pipelined\", \"startTimeUtc\": \"2025-01-06T00:12:22.8311867Z\", \"durationInMs\": 0.1507},{\"event\": \"Transit Time\", \"startTimeUtc\": \"2025-01-06T00:12:22.8313374Z\", \"durationInMs\": 0.7515},{\"event\": \"Received\", \"startTimeUtc\": \"2025-01-06T00:12:22.8320889Z\", \"durationInMs\": 0.0735},{\"event\": \"Completed\", \"startTimeUtc\": \"2025-01-06T00:12:22.8321624Z\", \"durationInMs\": 0}],\"serviceEndpointStats\":{\"inflightRequests\":1,\"openConnections\":1},\"connectionStats\":{\"waitforConnectionInit\":\"False\",\"callsPendingReceive\":0,\"lastSendAttempt\":\"2025-01-06T00:12:22.7832281Z\",\"lastSend\":\"2025-01-06T00:12:22.7832850Z\",\"lastReceive\":\"2025-01-06T00:12:22.7847877Z\"},\"requestSizeInBytes\":545,\"requestBodySizeInBytes\":40,\"responseMetadataSizeInBytes\":183,\"responseBodySizeInBytes\":53};\r\n ResourceType: Document, OperationType: Upsert\r\n, SDK: Microsoft.Azure.Documents.Common/2.14.0"}
Amostra reproduzível
https://github.com/EntityAdam/FunctionCrudSample/tree/features/serialization-problem-repro