michelcedric/StructuredMinimalApi

[Question] Dependency Injection Scoped

Closed this issue ยท 2 comments

Thanks for the nice and understandable library for Endpoints ๐Ÿ‘

I'm facing an issue with the library, and I believe this is related to DI-scoped visibility. But couldn't figure out if an issue on my side or not.
Although I haven't had an issue while a simple static class was used ๐Ÿค” code

Down below I'll explain the conditions and provide an example repo.

Having a simple Minimal API service

  • connecting to DB through static class and use service.AddScoped... code
  • Endpoints classes having Services/Mappers as DI trough controller - code

Everything works fine, until I run StressTest with 10 consecutive connections - immediately after 2 requests I got an error -
There is already an open DataReader associated with this Connection which must be closed first.

Repo - https://github.com/artem-galas/ComicsShop/tree/master/ComicsShop.Rest

Steps to reproduce:

  • run DB docker image
  • run application in docker
  • install Deps in StressTest folder (npm i)
  • run test npm run test-rest code

Appreciate your help :)

Hi
The endpoint class is instantiate only once.
If you store your service and by extension your repository in the endpoint class, the same instance it's always use. That provide your issue.
In the route definition, you must pass the service as parameter
Example
https://github.com/michelcedric/ComicsShop/blob/master/ComicsShop.Rest/Endpoints/GetAllComics.cs
Result : 178k query in 60 seconds (release mode without debuging)

I will think about a way to authorize your usage. But it's works ;)

Thanks for your help!