Transient component is registered as transient but implements idisposable

痞子三分冷 提交于 2019-12-11 06:01:44

问题


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

回答1:


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 implement IDisposable at all in your repository assuming the current implementations won't change much.



来源:https://stackoverflow.com/questions/53484944/transient-component-is-registered-as-transient-but-implements-idisposable

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