StartTime does not work if DateTime.Kind is not Utc
See original GitHub issueWe are continuously addressing and improving the SDK, if possible, make sure the problem persist in the latest SDK version.
Describe the bug
This could be a feature but I thought I should report it in case it’s not. When specifying the WithStartTime
, if I pass a DateTime with Kind Local
, the ChangeFeedProcessor is created and lease is saved in the container but it doesn’t pick up any message even though there are items that are newer than the specified date. It only starts working if I set the value with a DateTime with Utc
kind.
To Reproduce The code that’s not working
var startDateTimeString = "2022-11-08T14:28:33.000Z";
DateTime.TryParse(startDateTimeString, out DateTime startTime); // startTime.Kind is Local
var documentEndpoint = "https://someaccount.documents.azure.com:443/";
var databaseId = "ToDoList";
var targetContainerName = "Items";
var leaseContainerName = "leases";
var client = new CosmosClient(documentEndpoint, new DefaultAzureCredential());
var container = client.GetContainer(databaseId, targetContainerName);
var leaseContainer = _client.GetContainer(databaseId, leaseContainerName);
var processorName = $"manualChangeFeedProcessor{DateTime.UtcNow:O}";
var changeFeedProcessor = container
.GetChangeFeedProcessorBuilder<Document>(processorName: processorName, HandleChangesAsync)
.WithInstanceName($"consoleHost{DateTimeOffset.UtcNow.DateTime:O}")
.WithLeaseContainer(leaseContainer)
.WithStartTime(startTime)
.WithErrorNotification(HandleError)
.WithPollInterval(TimeSpan.FromMilliseconds(500))
.Build();
await changeFeedProcessor.StartAsync();
The code that’s working: (I only need to call .ToUniversalTime() on startTime
var startDateTimeString = "2022-11-08T14:28:33.000Z";
DateTime.TryParse(startDateTimeString, out DateTime startTime); // startTime.Kind is Local
var documentEndpoint = "https://someaccount.documents.azure.com:443/";
var databaseId = "ToDoList";
var targetContainerName = "Items";
var leaseContainerName = "leases";
var client = new CosmosClient(documentEndpoint, new DefaultAzureCredential());
var container = client.GetContainer(databaseId, targetContainerName);
var leaseContainer = _client.GetContainer(databaseId, leaseContainerName);
var processorName = $"manualChangeFeedProcessor{DateTime.UtcNow:O}";
var changeFeedProcessor = container
.GetChangeFeedProcessorBuilder<Document>(processorName: processorName, HandleChangesAsync)
.WithInstanceName($"consoleHost{DateTimeOffset.UtcNow.DateTime:O}")
.WithLeaseContainer(leaseContainer)
.WithStartTime(startTime.ToUniversalTime())
.WithErrorNotification(HandleError)
.WithPollInterval(TimeSpan.FromMilliseconds(500))
.Build();
await changeFeedProcessor.StartAsync();
Expected behavior It should either use given DateTime or throw error because I have given a DateTime that’s not Utc kind
Actual behavior It accepts given datetime but doesn’t process the changefeed.
Environment summary SDK Version: 3.31.2 OS Version: Windows
Additional context Add any other context about the problem here (for example, complete stack traces or logs).
Issue Analytics
- State:
- Created 10 months ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
@ealsur I found a way to work around this by manually calling GetChangeFeedProcessorBuilder in my function app startup. This allows me to manually create the change feed lease with a DateTime instance that has the correct UTC style. After that’s finished, the function app fires up and starts processing the change feed normally.
you’re right, DateTime.TryParse does parse it to
Local
by default for some reason. I agree with your concern that throwing exception to existing users would be a problem. I think .ToUniversalTime() would just work nicely.