c# - Unity IoC Lifetime per HttpRequest for UserStore -


i'm trying clean default implementation of accountcontroller.cs comes out of box in new mvc5/owin security implementation. have modified constructor this:

private usermanager<applicationuser> usermanager; public accountcontroller(usermanager<applicationuser> usermanager) {     this.usermanager = usermanager; } 

also, have created lifetime manager unity looks this:

 public class httpcontextlifetimemanager<t> : lifetimemanager, idisposable     {         private httpcontextbase _context = null;         public httpcontextlifetimemanager()         {             _context = new httpcontextwrapper(httpcontext.current);         }         public httpcontextlifetimemanager(httpcontextbase context)         {             if (context == null)                 throw new argumentnullexception("context");             _context = context;         }         public void dispose()         {             this.removevalue();         }         public override object getvalue()         {             return _context.items[typeof(t)];         }         public override void removevalue()         {             _context.items.remove(typeof(t));         }         public override void setvalue(object newvalue)         {             _context.items[typeof(t)] = newvalue;         }     } 

i'm not sure how write in unityconfig.cs, have far:

container.registertype<usermanager<applicationuser>>(new httpcontextlifetimemanager(new usermanager<applicationuser>(new userstore<applicationuser>(new recipemanagercontext())))); 

i did find example (using autofac) way:

container.register(c => new usermanager<applicationuser>(new userstore<applicationuser>( new recipemanagercontext())))                 .as<usermanager<applicationuser>>().instanceperhttprequest(); 

how translate above statement using unity ioc lifetime management?

your approach usermanager registered specific lifetime correct. however, don't understand why compiles, since httpcontextlifetimemanager expects httpcontext parameter.

another issue implementation wrong. parameterless constructor takes current http context, rather want lifetime manager use context instance created on rather 1 type registered on. if parameterless constructor used, lead http context mismatch issues.

first, change implementation

public class httpcontextlifetimemanager : lifetimemanager {  private readonly object key = new object();   public override object getvalue()  {      if (httpcontext.current != null &&           httpcontext.current.items.contains(key))          return httpcontext.current.items[key];      else          return null;  }   public override void removevalue()  {      if (httpcontext.current != null)          httpcontext.current.items.remove(key);  }   public override void setvalue(object newvalue)  {      if (httpcontext.current != null)          httpcontext.current.items[key] = newvalue;  } } 

and register type

container.registertype<usermanager<applicationuser>>( new httpcontextlifetimemanager() ); 

Comments

Popular posts from this blog

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -

Change the color of an oval at click in Java AWT -