瞬态组件已注册为瞬态,但实现了偶发 [英] Transient component is registered as transient but implements idisposable
问题描述
我使用简单注入器,但是以某种方式出现错误,如本主题所述.在决定发布此问题之前,我尝试了很多事情,例如,这对我不起作用.调用 Validate
时出现以下异常:
验证错误消息:
配置无效.报告了以下诊断警告:
-[一次性瞬态组件] AlbumsController已注册为瞬态,但实现了IDisposable.
-[一次性瞬态组件] ArtistsController已注册为瞬态,但实现了IDisposable.
-[一次性瞬态组件] HomeController已注册为瞬态,但实现了IDisposable.
有关警告的详细信息,请参见Error属性.请参阅
引发的异常我尝试将
Lifestyle.Scoped
添加到我的注册中,如下所示:container.Register< IRepository< Artist> ;, ArtistRepository>(Lifestyle.Scoped);container.Register< IRepository<专辑" ;、 AlbumRepository>(Lifestyle.Scoped);
控制器:
注意:存储库实现了
IDisposable
.公共类HomeController:控制器{...}公共类AlbumsController:控制器{只读IRepository< Album>_repository;公共AlbumsController(IRepository< Album>存储库){_repository =储存库;}...}
其他详细信息:
- 我的控制器不会不覆盖受
保护的虚拟void Dispose(bool)
方法.- 我正在使用Simple Injector v4.4.0
- 我正在使用Web集成软件包v3.2.7
解决方案基于提供的演示项目,我确实能够重现该问题.
问题出在所引用的软件包中:
< package id ="SimpleInjector" version ="4.4.0" targetFramework ="net46"/>< package id ="SimpleInjector.Integration.Web" version ="3.2.7" targetFramework ="net46"/>< package id ="SimpleInjector.Integration.Web.Mvc"版本="3.2.7" targetFramework ="net46"/>
如您所见,MVC的软件包来自另一个主要分支.
如果将所有内容更新为4.4.0,您的问题就会消失.
除此之外,错误消息是 not 告诉您在存储库中实现
IDisposable
.SimpleInjector和其他任何与此相关的工具都无法为您决定.如果需要,您应该自己决定.如果您还是注入
DbContext
(无论如何都是最佳实践),则假设当前的实现不会有太大变化,则无需在存储库中完全实现IDisposable
.I use Simple injector but somehow i get error as stated in subject. I tried many things before i decided to post this question for instance this is not working for me. I get the following exception when I call
Validate
:Validation error message:
The configuration is invalid. The following diagnostic warnings were reported:
-[Disposable Transient Component] AlbumsController is registered as transient, but implements IDisposable.
-[Disposable Transient Component] ArtistsController is registered as transient, but implements IDisposable.
-[Disposable Transient Component] HomeController is registered as transient, but implements IDisposable.
See the Error property for detailed information about the warnings. Please see https://simpleinjector.org/diagnostics how to fix problems and how to suppress individual warnings.
Application_Start
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); container = new Container(); container.Options.DefaultScopedLifestyle = new WebRequestLifestyle(); container.Register<IRepository<Artist>, ArtistRepository>(Lifestyle.Scoped); container.Register<IRepository<Album>, AlbumRepository>(Lifestyle.Scoped); container.RegisterMvcControllers(Assembly.GetExecutingAssembly()); container.Verify(); DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container)); }
I tried to add
Lifestyle.Scoped
to my registrations as follows:container.Register<IRepository<Artist>, ArtistRepository>(Lifestyle.Scoped); container.Register<IRepository<Album>, AlbumRepository>(Lifestyle.Scoped);
Controllers:
Note: Repositories implement
IDisposable
.public class HomeController : Controller { ... } public class AlbumsController : Controller { readonly IRepository<Album> _repository; public AlbumsController(IRepository<Album> repository) { _repository = repository; } ... }
Other details:
- My controllers do not override the
protected virtual void Dispose(bool)
method.- I'm using Simple Injector v4.4.0
- I'm using the web integration packages v3.2.7
解决方案Based on the supplied demo project I was indeed able to reproduce the issue.
The problem is in the referenced packages:
<package id="SimpleInjector" version="4.4.0" targetFramework="net46" /> <package id="SimpleInjector.Integration.Web" version="3.2.7" targetFramework="net46" /> <package id="SimpleInjector.Integration.Web.Mvc" version="3.2.7" targetFramework="net46" />
As you can see the packages for MVC are from another major branch.
If you update everything to 4.4.0 your problem goes away.
Apart from this, the error message is not telling you to implement
IDisposable
in your repositories. SimpleInjector, and any other tool for that matter, is not able to decide this for you.You should decide for yourself if you need that. If you would also inject the
DbContext
, which is best practice anyway, there is no need to implementIDisposable
at all in your repository assuming the current implementations won't change much.这篇关于瞬态组件已注册为瞬态,但实现了偶发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!