Simple Injector Diagnostic Warning Disposable Transient

女生的网名这么多〃 提交于 2019-12-30 18:44:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!