phi*_*hil 5 c# .net-core azure-cosmosdb
我已按照这些说明与 CosmosDb v3Newtonsoft一起在我的 Blazor Server 项目中使用。
因此我Startup.cs指定了几个设置和转换器。
services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.Formatting = Formatting.Indented;
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
options.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore;
options.SerializerSettings.Converters.Add(new ISubfileConverter());
options.SerializerSettings.Converters.Add(new ISubunitConverter());
options.SerializerSettings.Converters.Add(new IElementConverter());
options.SerializerSettings.Converters.Add(new IMetadataConverter());
});
services.AddSingleton<ICosmosSrcDocService>(InitializeCosmosSrcClientInstance(Configuration.GetSection("SourceCosmosDb"))
.GetAwaiter().GetResult());
Run Code Online (Sandbox Code Playgroud)
然而,当我使用UpsertItemAsync()它时,它似乎没有使用它。
有没有办法告诉 CosmosDbStartup.cs 使用此配置?
使用 Cosmos 配置更新
CosmosClientBuilder clientBuilder = new CosmosClientBuilder(account, key);
CosmosClient client = clientBuilder
.WithConnectionModeDirect()
.Build();
CosmosTrgDocService cosmosDbService = new CosmosTrgDocService(client, databaseName, containerName);
Run Code Online (Sandbox Code Playgroud)
我发现CosmosClientBuilder有一个WithCustomSerializer()方法。这让我发现您可以从中派生一个类CosmosSerializer,然后将其传递给该WithCustomSerializer()方法。
当您重写ToStream<T>和FromStream<T>方法时,您可以控制序列化。另请参阅此示例。
| 归档时间: |
|
| 查看次数: |
2209 次 |
| 最近记录: |