You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MaterializationInterceptors causes More than twenty 'IServiceProvider' instances
I have an specific use case for MaterializationInterceptors in an Net 8.0 web api.
I need to add IUserFactory to my Workgroup class.
here is my Workgroup class:
public class Workgroup
{
...
public IUserFactory UserFactory { get; set; }
}
and here is my UserFactory implementation:
public class UserFactory : IUserFactory
{
public UserFactory(Context context) { ... }
public void Create(string username, ...) { context.AddUser() // for example }
}
here is my Interceptor:
public class UserFactoryInterceptor : IMaterializationInterceptor
{
public object InitializedInstance(MaterializationInterceptionData materializationData, object instance)
{
if (instance is Workgroup entity)
{
entity.UserFactory = materializationData.Context.GetService<IUserFactory>();
}
return instance;
}
}
I have provided minimal example of this issue above that cause following exception after 20 call of an web api which make use of an instance of the Workgroup class.
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'.
Even though I've removed Context completely from UserFactory class this exception is still being raised.
I think the Interceptor is registered as singleton so multiple instance of my Context are refrenced this interceptor.
Tnx for any suggestion in advance 🌹
**UPDATE: **
Here is my Context:
public class Context : DbContext
{
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
@Reza-Noei Yes, this is a singleton interceptor; it generally should not hold state and the same instance should be used for all context instances. We need to document this better.
ajcvickers
changed the title
MaterializationInterceptors causes More than twenty 'IServiceProvider' instances
Document materialization interceptors are singleton
May 15, 2024
MaterializationInterceptors causes More than twenty 'IServiceProvider' instances
I have an specific use case for
MaterializationInterceptors
in anNet 8.0
web api.I need to add
IUserFactory
to myWorkgroup
class.here is my
Workgroup
class:and here is my
UserFactory
implementation:here is my Interceptor:
I have provided minimal example of this issue above that cause following exception after 20 call of an web api which make use of an instance of the
Workgroup
class.Even though I've removed
Context
completely fromUserFactory
class this exception is still being raised.I think the Interceptor is registered as singleton so multiple instance of my
Context
are refrenced this interceptor.Tnx for any suggestion in advance 🌹
**UPDATE: **
Here is my Context:
and my DI for the interceptor:
Installed Packages
EF Core version: 8.0.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system:
IDE: Visual Studio 2022 17.9.6
Microsoft.EntityFrameworkCore: Version="8.0.4"
Microsoft.EntityFrameworkCore.Design: Version="8.0.4"
Microsoft.EntityFrameworkCore.Relational: Version="8.0.4"
Microsoft.EntityFrameworkCore.SqlServer: Version="8.0.4"
Microsoft.EntityFrameworkCore.Tools: Version="8.0.4"
The text was updated successfully, but these errors were encountered: