I have an .NET 7 REST API which has singleton class initialization.
It is working fine with single instance, and it fails with multiple instances.
Below is my singleton initialization.
Engine _engine = new Engine(loggerFactory);
builder.Services.AddSingleton<Engine>(_engine);
This functionality mainly deletes/load folder to the wwwroot directory.
Could someone give me a solution on making use of multiple instances with singleton class.
2
Answers
Issue is fixed by dropping the files under the machine name
Ensuring to run only single instance of the Azure Function App runs at a time is the responsibility of
SingletonAttribute
class.This class also other feature in limiting the instances count that runs at that time and that impacts the scalability and performance.
That feature is the
Mode
property of theFunctionInvocation
attribute and the syntax is:Mode = Function / Listener
If the mode property is
Listener
, then it does the single instance execution andFunction
mode can give you run multiple instances and simultaneous executions but ensures that only one instance processes a given function invocation at a time.Refer to this MS Doc and medium article by the author @yu_ka1984 for more information on using the
SingletonAttribute
–FunctionInvocation
–Mode
for running the multiple instances.