ASP.NET MVC: unity and HierarchicalLifeTimemanager -


i have simple asp.net mvc application. added unity project , tested different lifetime managers. know hierarchicallifitememanager containercontrolledlifetimemanager (singletone) unity creates different instances each child unity container. created class single guid property set in constructor, injected class in controller using hierarchicallifitememanager , show guid in view. , every time press f5 see new guid. how hierarchicallifitememanager work?

it works fine. see different guid because create new root container each request (f5). try this:

class hierarchicalservice {     public guid value {get;set;}     public hierarchicaservice()     {         value = guid.guid.newguid();     } }  register it: ... container.registertype<hierarchicalservice>(new hierarchicallifitememanager()); ...  class mycontroller {     mycontroller(hierarchicalservice servicea, hierarchicalservice serviceb)     {         //...compare values both services     } }  , compare this:  class mycontroller {     mycontroller(iunitycontainer container)     {         var servicea = container.createchildcontainer().resolve<hierarchicalservice>();         var serviceb = container.createchildcontainer().resolve<hierarchicalservice>();         //...compare values both services     } } 

so, difference in case, when have 1 root container , need separate actions creating new child containers isolated scopes (unitofworks, example). read more child containers.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -