问题
I am trying to configure simple injector to work, but I can't seem to get pass this. I followed the instructions on how to fix this in this link but it didn't work. Here is the error message:
NotificationEntities is registered as transient, but implements IDisposable.
Here is the code to the SimpleInjectorInitializer.cs
public static void Initialize()
{
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
InitializeContainer(container);
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}
private static void InitializeContainer(Container container)
{
container.Register<IEmailTemplateRepository, EmailTemplateRepository>();
}
The NotificationEntities is generated from EF so it should already implement that, correct?
回答1:
The NotificationEntities is generated from EF so it should already implement that, correct?
Yes, NotificationEntities
does implement IDisposable
, and this is exactly what the warning is telling you:
A registration has been made with the Transient lifestyle for a component that implements IDisposable.
This is a problem because:
A component that implements
IDisposable
would usually need deterministic clean-up but Simple Injector does not implicitly track and dispose components registered with the transient lifestyle.
To fix this you should:
Register the component with the scoped lifestyle that is appropriate for the application you are working on.
In other words, register your NotificationEntities
as follows:
container.Register<NotificationEntities>(Lifestyle.Scoped);
来源:https://stackoverflow.com/questions/42591234/simple-injector-diagnostic-warning-disposable-transient