question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serializer customization using `ICosmosDBSerializerFactory` does not work

See original GitHub issue

I 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)

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ealsurcommented, Sep 10, 2021

Should be fixed in preview2 release

1reaction
ealsurcommented, Sep 3, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found