CosmosDB 每项 ttl 设置抛出异常:无法反序列化当前 JSON 对象
CosmosDB per item ttl setting throwing exception: Cannot deserialize the current JSON object
我正在关注 this documentation 以将每个项目 ttl
设置为 CosmosDB table 条目。但是当我在实体 class 中添加字段名称 ttl
时,我在进行 Insert/Replace 调用时遇到以下错误:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Nullable`1[System.Int32]' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.
To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'ttl.$t', line 1, position 109.
public class MyEntity : TableEntity
{
public string Prop { get; set; }
[JsonProperty(PropertyName = "ttl", NullValueHandling = NullValueHandling.Ignore)]
public int? ttl { get; set; }
public MyEntity(
string pk,
string rk,
string prop)
{
this.PartitionKey = pk;
this.RowKey = rk;
this.Prop =prop;
this.ttl = -1;
}
}
如何解决?
更新
关注官方文档更新我的Azure Cosmos DB Emulator
。项目将根据 TTL
.
的预期值被删除
while (feedIterator.HasMoreResults)
{
foreach (var item in await feedIterator.ReadNextAsync())
{
item.ttl = 10;
await container.UpsertItemAsync<MyEntity>(item, new PartitionKey(item.address));
}
}
希望我的回答能帮到您
using System;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using Newtonsoft.Json;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Table;
namespace CosmosGettingStartedTutorial
{
class Program
{
// <Main>
public static async Task Main(string[] args)
{
try
{
Console.WriteLine("Beginning operations...\n");
CosmosClient client = new CosmosClient("https://localhost:8081/", "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");
Database database = await client.CreateDatabaseIfNotExistsAsync("ToDoList");
Container container = database.GetContainer("jason");
// Add for an item
QueryDefinition queryDefinition = new QueryDefinition("select * from c ");
FeedIterator<MyEntity> feedIterator = container.GetItemQueryIterator<MyEntity>(queryDefinition, null, new QueryRequestOptions() { PartitionKey = new PartitionKey("address0") });
int count = 0;
while (feedIterator.HasMoreResults)
{
foreach (var item in await feedIterator.ReadNextAsync())
{
if (item.id == "0")
{
Console.WriteLine("id equal 0 is exist: id = " + item.id);
Console.WriteLine("We will change id='test" + item.id + "'");
Console.WriteLine("pls wait ,will update ");
item.id = "test" + item.id;
await container.UpsertItemAsync<MyEntity>(item, new PartitionKey(item.address));
}
count++;
}
}
int num = count + 5;
for (int i = count; i < num; i++)
{
MyEntity entity = new MyEntity(i.ToString(), i.ToString(), i.ToString());
entity.id = i.ToString();
entity.address = "address0";
await container.CreateItemAsync<MyEntity>(entity, new PartitionKey(entity.address));
}
}
catch (CosmosException de)
{
Exception baseException = de.GetBaseException();
Console.WriteLine("{0} error occurred: {1}", de.StatusCode, de);
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
}
finally
{
Console.WriteLine("End of demo, press any key to exit.");
Console.ReadKey();
}
}
public class MyEntity : TableEntity
{
public string Prop { get; set; }
[JsonProperty(PropertyName = "ttl", NullValueHandling = NullValueHandling.Ignore)]
public int? ttl { get; set; }
public MyEntity(string pk, string rk, string prop)
{
this.PartitionKey = pk;
this.RowKey = rk;
this.Prop = prop;
this.ttl = -1;
}
public string address { get; set; }
public string id { get; set; }
}
}
}
我正在关注 this documentation 以将每个项目 ttl
设置为 CosmosDB table 条目。但是当我在实体 class 中添加字段名称 ttl
时,我在进行 Insert/Replace 调用时遇到以下错误:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Nullable`1[System.Int32]' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly. To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'ttl.$t', line 1, position 109.
public class MyEntity : TableEntity
{
public string Prop { get; set; }
[JsonProperty(PropertyName = "ttl", NullValueHandling = NullValueHandling.Ignore)]
public int? ttl { get; set; }
public MyEntity(
string pk,
string rk,
string prop)
{
this.PartitionKey = pk;
this.RowKey = rk;
this.Prop =prop;
this.ttl = -1;
}
}
如何解决?
更新
关注官方文档更新我的Azure Cosmos DB Emulator
。项目将根据 TTL
.
while (feedIterator.HasMoreResults)
{
foreach (var item in await feedIterator.ReadNextAsync())
{
item.ttl = 10;
await container.UpsertItemAsync<MyEntity>(item, new PartitionKey(item.address));
}
}
希望我的回答能帮到您
using System;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using Newtonsoft.Json;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Table;
namespace CosmosGettingStartedTutorial
{
class Program
{
// <Main>
public static async Task Main(string[] args)
{
try
{
Console.WriteLine("Beginning operations...\n");
CosmosClient client = new CosmosClient("https://localhost:8081/", "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");
Database database = await client.CreateDatabaseIfNotExistsAsync("ToDoList");
Container container = database.GetContainer("jason");
// Add for an item
QueryDefinition queryDefinition = new QueryDefinition("select * from c ");
FeedIterator<MyEntity> feedIterator = container.GetItemQueryIterator<MyEntity>(queryDefinition, null, new QueryRequestOptions() { PartitionKey = new PartitionKey("address0") });
int count = 0;
while (feedIterator.HasMoreResults)
{
foreach (var item in await feedIterator.ReadNextAsync())
{
if (item.id == "0")
{
Console.WriteLine("id equal 0 is exist: id = " + item.id);
Console.WriteLine("We will change id='test" + item.id + "'");
Console.WriteLine("pls wait ,will update ");
item.id = "test" + item.id;
await container.UpsertItemAsync<MyEntity>(item, new PartitionKey(item.address));
}
count++;
}
}
int num = count + 5;
for (int i = count; i < num; i++)
{
MyEntity entity = new MyEntity(i.ToString(), i.ToString(), i.ToString());
entity.id = i.ToString();
entity.address = "address0";
await container.CreateItemAsync<MyEntity>(entity, new PartitionKey(entity.address));
}
}
catch (CosmosException de)
{
Exception baseException = de.GetBaseException();
Console.WriteLine("{0} error occurred: {1}", de.StatusCode, de);
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
}
finally
{
Console.WriteLine("End of demo, press any key to exit.");
Console.ReadKey();
}
}
public class MyEntity : TableEntity
{
public string Prop { get; set; }
[JsonProperty(PropertyName = "ttl", NullValueHandling = NullValueHandling.Ignore)]
public int? ttl { get; set; }
public MyEntity(string pk, string rk, string prop)
{
this.PartitionKey = pk;
this.RowKey = rk;
this.Prop = prop;
this.ttl = -1;
}
public string address { get; set; }
public string id { get; set; }
}
}
}