I'm using the service collection class to build a service provider that is used for my lambda. I don't really want to dispose of all my singletons every time the function handler is called, but I do want to dispose of them whenever the lambda is spinning down.
It's my understanding that without this, there's a chance that some unmanaged connections may not be closed correctly. To avoid that problem, I added a destructor which calls .Dispose() on the service provider.
How can I be sure that the lambdas are exited gracefully enough that my destructor will be called every time, and is my understanding that it can be beneficial for unmanaged resources correct?
Sample code:
public class Function {
private readonly ServiceProvider _provider;
public Function() {
_provider = new ServiceCollection.AddXyz();
}
~Function() {
_provider.Dispose();
}
}
The handler is omitted but just know that it's opening database connections, rabbit mq connections, etc. all via the service collection container, and I don't want to wrap it in a using statement with a scope because if there are multiple calls that come in for a single lifetime of the lambda, I want the same connections to be used.