Performance DI AddScope in web api layer of Mobile App

Viewed 34

So i created a mobile app that mobile app hooks into this web api hosted on a server but I was just thinking how I did the startup class and in their I have a load of services I am calling in the web api.

Is Using Add Scope bad in this if multiple users will start hitting the system.

services.AddScoped<IteamInterface,TeamService>();
services.AddScoped<IPlayerInterface, PlayerService>();
services.AddScoped<IProfileInterface, ProfileService>();
services.AddScoped<IWorkoutsInterface, WorkoutsService>();

Typical service would be

public class WorkoutsService : IWorkoutsInterface
{
    APPDBContext db;
    public WorkoutsService(APPDBContext dBContext)
    {
        db = dBContext;

    }
    public void AddWorkOut(Workout workout)
    {
        db.WorkOuts.Add(workout);
        db.SaveChanges();
    }
  ... DO more db calls
}

It wont be heavy hit the application but could using add scope cause me an issue further down the road.

I have about 15 services in startup.cs being loaded this way and just concerend if about 4 coaches will be using this latter

1 Answers

Scoped is the correct lifetime choice in your case. While it might seem that going with Singleton lifetime is a better choice due to reduced GC pressure (you would not be recreating these services on every request, as is currently the case), you might create a bottleneck on the database access side, queuing all sessions to use a single DbContext instance.

Keep your app as-is; if you encounter performance issues, profile carefully to find the true cause of the issue, and adjust accordingly.

Related