Serializer customization using `ICosmosDBSerializerFactory` does not work
See original GitHub issueI created a class that implements ICosmosDBSerializerFactory
and tried to customize the default serializer, but it did not work correctly because CreateSerializer
was not called.
It seems that after adding the customized ICosmosDBSerializerFactory
to the DI, the Cosmos DB Extension overrides it with the default SerializerFactory.
The initialization by Startup seems to be done in the order of Function App
=> Extensions
, so if the Extension provides an extensible interface, it need to use TryAddSingleton
. (In Function Extensions, it could be a convention to use TryAddSingleton
)
- https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/storage/Microsoft.Azure.WebJobs.Extensions.Storage.Blobs/src/StorageBlobsWebJobsBuilderExtensions.cs
- https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/storage/Microsoft.Azure.WebJobs.Extensions.Storage.Queues/src/StorageQueuesWebJobsBuilderExtensions.cs
Repro steps
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<ICosmosDBSerializerFactory, MyCosmosDBSerializerFactory>();
}
}
public class MyCosmosDBSerializerFactory : ICosmosDBSerializerFactory
{
public CosmosSerializer CreateSerializer()
{
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
// From https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs
return new CosmosSystemTextJsonSerializer(options);
}
}
Expected behavior
The customized SerializerFactory will be used.
Actual behavior
The default implementation of SerializerFactory is always used.
Known workarounds
Not available.
Related information
Microsoft.Azure.WebJobs.Extensions.CosmosDB
:4.0.0-preview1
- https://github.com/Azure/azure-webjobs-sdk-extensions/blob/dev/src/WebJobs.Extensions.CosmosDB/Config/CosmosDBWebJobsBuilderExtensions.cs#L36
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Azure Functions v4 serialize to Cosmos DB in camel case
Assuming you have a Functions Startup, you can inject your own custom ICosmosDBSerializerFactory implementation that uses your own custom ...
Read more >Under the hood of the new Azure Functions extension for ...
UseDefaultJsonSerialization is no longer needed, you can fully customize the serialization if you want; ConnectionStringSetting is now ...
Read more >Custom JSON serialization with Azure Cosmos DB SDK
This post explains how you can use custom JSON serializer settings with Cosmos DB using Newtonsoft JSON and System.Text.Json.
Read more >Cosmos db functions. Also, please check the FireWall settings ...
Assuming you have a Functions Startup, you can inject your own custom ICosmosDBSerializerFactory implementation that uses your own custom CosmosSerializer.
Read more >Azure Functions v4 serialize to Cosmos DB in camel case
Assuming you have a Functions Startup, you can inject your own custom ICosmosDBSerializerFactory implementation that uses your own custom CosmosSerializer .
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Should be fixed in preview2 release
Yeah, I added a test with the inverted order (injection first then AddCosmosDB) and that initially failed (that’s how I came to the conclusion about the order), with the TryAddSingleton change, then both tests passed. Thanks for the pointer to the other examples, that was very useful.