Question

I needed information about when and how is the ASP.NET Membership Provider created and also about its lifetime and garbage collection. I tried looking for the same but wasn't able to find concrete information.

Was it helpful?

Solution

Lifetime

Membership provider is based on provider model which is a singleton.

Provider's Initialize method is called only if the application uses/calls one of its methods. Once the provider is initialized, it uses the same settings until the application is disposed.

In other words, if users browse only publicly available pages, they won't need to login, and the application will never get chance to initialize Membership provider at all.

garbage collection

Provider releases memory when application ends. .NET Framework manages garbage collection. (I might be wrong; I cannot answer this question)

OTHER TIPS

I have not created a CustomMembershipProvider personally (using MVC4 based SimpleMembershipProvider). However looking at the information around can tell you that when you implement your CustomMembershipProvider deriving from MembershipProvider, you will implement below method.

public override void Initialize(string name, NameValueCollection config)

Initialize() method will be called by your application root (unless you want to control lifetime based on your different need).

For example if you create a new MVC4 web application internet template in VS2012, You will see that AccountController has an attribute "InitializeSimpleMembershipAttribute" applied to it. This attribute ensure that the Membership provider is initialize and initialize only once per application lifetime.

You may see this initialization done in different way e.g. Register your attribute in RegisterGlobalFilters() method which is setup during application startup in Global.asax.cs, or register a start method using and initialize membership provider there.

assembly: WebActivator.PreApplicationStartMethod

Based on how you initialize your custom membership provider, you will see when it is disposed off. If it is initialized during the application startup as part of application initialization code then it will be disposed of during application tear down or in a method that you register with "ApplicationShutdownMethodAttribute".

Please check this link for more information on how to create a custom membership provider, might be helpful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top