.net - How do I control MembershipProvider instance creation/lifetime? -


i have registered custom membershipprovider class in web.config file. i'm using inversion of control using castle windsor , have registered custom membershipprovider class transient (because it's using service that's transient well).

this means want membership provider instance recreated on every web request. currently, created once per application domain when tries access service depends on, service instance reused while not supposed to.

now need find way of having windsor control lifetime of custom membershipprovider don't know how. expected factory sitting around somewhere in .net framework, allowing me override instance creation , rerouting windsor can't find alike.

by way, i'm using .net 4.0.

update: here's of code can see i'm doing exactly:

web.config:

<membership defaultprovider="mymembershipprovider" >   <providers>     <clear/>     <add name="applicationmembershipprovider"          type="mynamespace.mymembershipprovider, myassembly"/>   </providers> </membership> 

membership provider

public class mymembershipprovider : membershipprovider {     private imyservice myservice;      public mymembershipprovider() : base()     {         // should use constructor injection here since cannot control         // construction of class, we're forced create dependency         // ourselves.     }      public override bool validateuser(string username, string password)     {         if (myservice == null)         {             // scope reached once within browser session,             // asp.net keeps instance of mymembershipprovider in memory             // myservice field keeps value across web requests.             // results in castle windsor (which have configured service             // locator use) not being able control lifetime of             // myservice instance. so, inability of windsor control             // lifetime of membershipprovider instances, inhibits lifetime             // management of myservice instances well.             myservice = servicelocator.current.getinstance<imyservice>();         }          return myservice.validateuser(username, password);     } } 

i blogged this solution.

in nutshell, solution involves simple, reusable membershipprovider calls container resolve custom membershipproviders. unlike other solutions use "buildup" container features, 1 takes true control of instantiation, enabling constructor injection (which in turn enables immutability) , proxyability.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -