I have a web job with CosmosDBTrigger
. I want to deserialize this Document
per my POCO
class, but I am getting 'S' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
error.
Approaches:
- I tried
var mappedObject = JsonSerializer.Deserialize<T>(document.ToString());
but received error. - Same with
var mappedObject = JsonConvert.DeserializeObject<Inspection>(document.ToString());
. This is withNewtonsoft
. - This question talks about
document.Resource.ToString()
but I don't even see.Resource
option. Is this related to the packages I am using? (see below)
Nuget Packages:
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.18.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="4.2.0" />
General Questions:
- Why
deleting
record is not triggeringCosmosDBTrigger
? - Why am I not seeing
.Resource
property inDocument
? - Can I see the DB action such as insert, update, delete on back end when
CosmosDBTrigger
is triggered?
Code:
Program.cs (snippet of adding cosmos in webjob)
var builder = new HostBuilder()
.UseEnvironment(environment)
.ConfigureWebJobs(b =>
{
b.AddCosmosDB(_ =>
{
_.ConnectionMode = ConnectionMode.Direct;
});
})
Listener.cs
public class Listener
{
private readonly ILogger<Listener> _logger;
public Listener(ILogger<Listener> logger)
{
_logger = logger;
}
public async Task ProcessDocumentAsync(
[CosmosDBTrigger(
databaseName: Configurations.DatabaseName,
containerName: Configurations.ContainerName,
Connection = Configurations.ConnectionString,
CreateLeaseContainerIfNotExists = true
)]
IReadOnlyList<Document> documents)
// If I change this to IReadOnlyList<MyContainerObject> documents then it works but I would also like to know if this is update,insert or delete by using `Document` properties. (Not sure if this is possible or not)
{
var totalDocuments = documents.Count;
_logger.Info(
$"[{nameof(Listener)}]: Received {Configurations.DatabaseName}/{Configurations.ContainerName}, deliveryCount:{totalDocuments}");
foreach (var document in documents)
{
await ProcessDocument(document);
}
}
public async Task ProcessDocument(Document document)
{
try
{
// Getting error here
var mappedObject = JsonSerializer.Deserialize<MyContainerObject>(document.ToString());
_logger.Info($"ProcessDocument: {{@document}}", args: new object[] { document });
}
catch (Exception exception)
{
_logger.Error(
$"Exception while processing : {{@exception}} document: {{@document}}",
args: new object[] { exception.GetBaseException(), document });
throw;
}
}
}