gpt4 book ai didi

c# - 简单的喷油器诊断警告一次性瞬变

转载 作者:太空狗 更新时间:2023-10-29 20:07:00 31 4
gpt4 key购买 nike

我正在尝试配置简单的注入(inject)器来工作,但我似乎无法通过它。我按照此 link 中有关如何解决此问题的说明进行了操作但它没有用。这是错误消息:

NotificationEntities is registered as transient, but implements IDisposable.

这是 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 确实实现了 IDisposable,这正是警告的内容 telling你:

A registration has been made with the Transient lifestyle for a component that implements IDisposable.

这是一个问题,因为:

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.

要解决此问题,您应该:

Register the component with the scoped lifestyle that is appropriate for the application you are working on.

换句话说,按如下方式注册您的NotificationEntities:

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

关于c# - 简单的喷油器诊断警告一次性瞬变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42591234/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com