How can I control the creation of an instance of MembershipProvider / lifetime?

I registered my own MembershipProvider class in my Web.Config file. I use Inversion Of Control with Castle Windsor and I registered my custom MembershipProvider class as transient (because it also uses a service that is also transient).

This means that I want the membership provider instance to be recreated in every web request. Currently, it is created only once for the application domain, so when it tries to access the service it depends on, this instance of the service is reused until it is supposed to.

Now I need to find a way to use Windsor for the life of my custom MembershipProvider element, but I don't know how to do it. I was expecting the factory to sit somewhere in the .NET Framework, allowing me to override instance creation and redirect it to Windsor, but I can't find anything like it.

By the way, I am using .NET 4.0.

UPDATE: Here are some of my codes so you can see what I am 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() { // We should use constructor injection here but since we cannot control // the construction of this class, we're forced to create the dependency // ourselves. } public override bool ValidateUser(string username, string password) { if (myService == null) { // This scope is only reached once within the browser session, // ASP.NET keeps the instance of MyMembershipProvider in memory // so the myService field keeps its value across web requests. // This results in Castle Windsor (which I have configured the service // locator to use) not being able to control the lifetime of // the MyService instance. So, the inability of Windsor to control // the lifetime of MembershipProvider instances, inhibits the lifetime // management of MyService instances as well. myService = ServiceLocator.Current.GetInstance<IMyService>(); } return myService.ValidateUser(username, password); } } 
+6
inversion-of-control castle-windsor asp.net-membership
source share
2 answers

I just wrote about it using a solution.

In short, this solution includes a simple reusable MemberhipProvider that calls a container to allow your custom MembershipProviders. Unlike other solutions that use the functions of the BuildUp container, this process has the right control over the instance, which allows you to enter a constructor (which, in turn, ensures immutability) and proxy security.

+4
source share

Do not worry about the life of your MembershipProvider. Just control the IMyService lifetime within the provider. Create a property for your IMyService using getter and return a new instance (or, nevertheless, you want to control the life cycle) each time it is requested.

+2
source share

All Articles