-
Some context to this before I give the example: See also these docs: https://docs.aws.amazon.com/lambda/latest/operatorguide/static-initialization.html As the docs only show this for python, this is how most people seem to do the equivalent in C#:
So typically from what I've seen the pattern is having a static field (often times a Dictionary for a in memory cache), or in this case it is an aws sdk client, and as we are connecting to the same account with the same credentials it shouldn't need to be instantiated per lambda execution. Unfortunately, we found an issue where this static Client field was being disposed of, so it would only work once per cold start, and every subsequent invocation it would already have been disposed and fail. Now, I think I know what we did wrong here but as I said the docs are only for python so I want to double check: Even though fields can be shared across warm starts, there is no guarantee. Which I guess means we need to check if its null each time, and instantiate it again just in case? i.e
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I'm missing what is disposing the service client? There is nothing in the Lambda programming model that would trigger a dispose. To be clear you don't have to do everything static. In .NET the Init phase includes running the constructor of the type that contains the function handler. |
Beta Was this translation helpful? Give feedback.
I'm missing what is disposing the service client? There is nothing in the Lambda programming model that would trigger a dispose.
To be clear you don't have to do everything static. In .NET the Init phase includes running the constructor of the type that contains the function handler.