-
Type:
Bug
-
Resolution: Done
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
None
-
None
-
Dotnet Drivers
-
None
-
None
-
None
-
None
-
None
-
None
The suggested Getting started setup on the GitHub page appears to be simple but sending a lot of requests will result in an exception because every time the DbContext is instantiated, it internally creates a new IServiceProvider. This is an example that I ran into earlier today while using the `PlanetScale.Create()` approach from the README.md.
2025-06-12 15:25:01.465 +00:00 [ERR] An unhandled exception has occurred while executing the request. System.AggregateException: One or more errors occurred. (An error was generated for warning 'Microsoft.EntityFrameworkCore.Infrastructure.ManyServiceProvidersCreatedWarning': More than twenty 'IServiceProvider' instances have been created for internal use by Entity Framework. This is commonly caused by injection of a new singleton service instance into every DbContext instance. For example, calling 'UseLoggerFactory' passing in a new instance each time--see https://go.microsoft.com/fwlink/?linkid=869049 for more details. This may lead to performance issues, consider reviewing calls on 'DbContextOptionsBuilder' that may require new service providers to be built. This exception can be suppressed or logged by passing event ID 'CoreEventId.ManyServiceProvidersCreatedWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.) ---> System.InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Infrastructure.ManyServiceProvidersCreatedWarning': More than twenty 'IServiceProvider' instances have been created for internal use by Entity Framework. This is commonly caused by injection of a new singleton service instance into every DbContext instance. For example, calling 'UseLoggerFactory' passing in a new instance each time--see https://go.microsoft.com/fwlink/?linkid=869049 for more details. This may lead to performance issues, consider reviewing calls on 'DbContextOptionsBuilder' that may require new service providers to be built. This exception can be suppressed or logged by passing event ID 'CoreEventId.ManyServiceProvidersCreatedWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'. at void Microsoft.EntityFrameworkCore.Diagnostics.EventDefinition.Log<TLoggerCategory>(IDiagnosticsLogger<TLoggerCategory> logger, Exception exception) at void Microsoft.EntityFrameworkCore.Diagnostics.CoreLoggerExtensions.ManyServiceProvidersCreatedWarning(IDiagnosticsLogger<Infrastructure> diagnostics, ICollection<IServiceProvider> serviceProviders) at IServiceProvider Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, bool providerRequired)+BuildServiceProvider(IDbContextOptions _, (ConcurrentDictionary<IDbContextOptions, ValueTuple<IServiceProvider, IDictionary<string, string>>>, IDbContextOptions) arguments) at IServiceProvider Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, bool providerRequired)+(IDbContextOptions contextOptions, (ConcurrentDictionary<IDbContextOptions, ValueTuple<IServiceProvider, IDictionary<string, string>>> _configurations, IDbContextOptions options) tuples) => { } at TValue System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>.GetOrAdd<TArg>(TKey key, Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument) at IServiceProvider Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, bool providerRequired) at new Microsoft.EntityFrameworkCore.DbContext(DbContextOptions options) at new MyApp.Catalog.MyDbContext(DbContextOptions<MyDbContext> options) in /home/vsts/work/1/s/src/MyApp.Catalog/MyDbContext.cs:line 23 at MyDbContext MyApp.Catalog.MyDbContext.Create(IMongoDatabase database) in /home/vsts/work/1/s/src/MyApp.Catalog/MyDbContext.cs:line 18 at MyDbContext MyApp.Catalog.MongoDbProvider.CreateContext() in /home/vsts/work/1/s/src/MyApp.Catalog/MongoDbProvider.cs:line 31 at async Task<List<ProductDTO>> MyApp.Core.API.Services.ProductService.GetAsync(int skip, int take, bool randomizeOrder, CancellationToken cancellationToken) in /home/vsts/work/1/s/src/MyApp.Core.API/Services/ProductService.cs:line 43 at async Task<IActionResult> MyApp.Core.API.Controllers.ProductsController.Get(int skip, int take, bool randomizeOrder, CancellationToken cancellationToken) in /home/vsts/work/1/s/src/MyApp.Core.API/Controllers/ProductsController.cs:line 41 at async ValueTask<IActionResult> Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+TaskOfIActionResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments) at async Task Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()+Awaited(?) at async Task Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()+Awaited(?) at void Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Task Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted) at Task Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() at async Task Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()+Awaited(?) at async Task Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeAsync()+Logged(?) x 2 at async Task Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)+AwaitRequestTask(?) --- End of inner exception stack trace --- at async Task Farfetch.LoadShedding.Limiters.AdaptativeConcurrencyLimiter.ExecuteAsync(Priority priority, Func<Task> function, CancellationToken cancellationToken) at async Task Farfetch.LoadShedding.AspNetCore.Middlewares.AdaptativeConcurrencyLimiterMiddleware.InvokeAsync(HttpContext context) at async Task Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at async Task Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at async Task Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at async Task Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at async Task Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context) at async Task Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddlewareImpl.Invoke(HttpContext context)+Awaited(?)