Unity: Change default lifetime manager for implicit registrations and/or disable them

后端 未结 2 403
忘了有多久
忘了有多久 2020-12-11 01:34

The Unity container will automatically resolve any type that it can figure out on its own without the need for manual registration. That\'s good in some ways, but the proble

2条回答
  •  时光说笑
    2020-12-11 02:24

    I tried the DefaultLifetimeManegerExtension but somehow it didnt work. (Maybe Unity has changed?). Anyways if you dont want to use an Extension you can also make use of the RegsiterByConvention feature.

    container.RegisterTypes(AllClasses.FromLoadedAssemblies(), WithMappings.None, WithName.Default, x => new DisposingTransientLifetimeManager());
    

    This will register all Classes with the "DisposingTransientLifetimeManager" which I chose to use as a default one. Note that you should do this at the beginning of your registrations since all registrations will overwrite any previous registrations of the same Type and Name.

提交回复
热议问题