C#C
C#6mo ago
GamerWoona

✅ Custom TimeProvider for WebApi

I am doing a lil api market simulation. Now, I'd like to run a custom time (sped up), for one, run events like budget reports on a quarter-year basis based on it.
From my initial research into the topic, running a custom TimeProvider seems fairly straightforward, though I don't think I can plug that one into EF to have the DateUpdated times in the DB match the in-app time.

What I am unsure about is how to go about the event chain. I was thinking of the TimeProvider running internal Timers based on the simulation speed (that need to be updated and maintained if it is changed) and sending out events based on these but maybe there is a better way.
Also, persisting the in-simulation time over sessions is something I am a bit unsure about. Would have loved to persist that on shutdown but that is probably unreliable but writing it into the db on every GetUtcNow call is a bit much, too?

Checking out the Clock Class, it didn't quite match what I was looking for, so I thought TimeProvider with a few extras could be good enough.

Maybe there is a pre-built solution for this but I am just too blind to see it.
Was this page helpful?