简单喷油器诊断警告一次性瞬态 [英] Simple Injector Diagnostic Warning Disposable Transient

查看:50
本文介绍了简单喷油器诊断警告一次性瞬态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试配置简单的喷油器以使其正常工作,但似乎无法通过。我在链接中遵循了有关如何解决此问题的说明。工作。这是错误消息:

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已注册为瞬态,但实现了
IDisposable。

NotificationEntities is registered as transient, but implements IDisposable.

这是SimpleInjectorInitializer.cs的代码

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>();
}

NotificationEntities是从EF生成的,因此它应该已经实现了,对吗?

The NotificationEntities is generated from EF so it should already implement that, correct?

推荐答案


NotificationEntities是从EF生成的,因此它应该已经实现了,对吗?

The NotificationEntities is generated from EF so it should already implement that, correct?

是, NotificationEntities 确实实现了 IDisposable ,这正是警告告诉您的意思:

Yes, NotificationEntities does implement IDisposable, and this is exactly what the warning is telling you:


已经为实现IDisposable的组件注册了Transient生活方式。

这是一个问题,因为:


实现 IDisposable 通常需要确定性的清理,但是Simple Injector不会隐式跟踪和处置临时生活方式中注册的组件。

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:


使用适当的范围内的生活方式注册组件

换句话说,注册您的 Notificat ionEntities 如下:

In other words, register your NotificationEntities as follows:

container.Register<NotificationEntities>(Lifestyle.Scoped);

这篇关于简单喷油器诊断警告一次性瞬态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆