Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Injector Diagnostic Warning Disposable Transient

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?

like image 800
Ron T Avatar asked Mar 04 '17 02:03

Ron T


1 Answers

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);
like image 85
Steven Avatar answered Nov 13 '22 07:11

Steven