DI Scope in Web APIs
fischja opened this issue · 2 comments
Hi there, I would like to host NsfwSpy inside a simple WebAPI controller to determine whether or not incoming images are NSFW. My question - if I register NsfwSpy in the DI system as Scoped, will new instances also consume resources? I.e. if one instance consumes ~600mb RAM, will 10 simultaneous incoming classification requests require ~6gb of RAM? Alternatively, can I safely register NsfwSpy as a DI Singleton without encountering threading issues or similar? What do you recommend as a best practice for this situation? Note - I am happy processing all incoming requests sequentially, my workload does not require parallel classification at this stage.
And thank you very much for providing this package!
Hey, thanks for the issue report 😄
For each instance of the NsfwSpy class you make, they should all share the same resources, so in your example 10 instances should only consume 600MB of RAM, whether that be as a scoped instance or as a singleton.
If you look at this file which we use in our NsfwSpy.App project to spin up a simple API to classify images, you can see that we register the NsfwSpy class as a Singleton:
builder.Services.AddSingleton<INsfwSpy, NsfwSpy>();
There shouldn't be any threading issues, however I haven't done extensive testing with multiple instances of the NsfwSpy class, so if you do come across any issues, just give me a shout and I can look into it.
Hope that helps! 😊
Thanks, will try it out!