I have registered a custom MembershipProvider class in my Web.Config file. I'm using Inversion Of Control using Castle Windsor and I have registered my custom MembershipProvider class as transient (because it's using a service that's transient as well).
This means that I want the Membership provider instance recreated on every web request. Currently, it is created only once per application domain so when it tries to access the service it depends on, that service instance is reused while it is not supposed to.
Now I need to find a way of having Windsor control the lifetime of my custom MembershipProvider but I don't know how. I expected a factory sitting around somewhere in the .NET Framework, allowing me to override the instance creation and rerouting it to Windsor but I can't find anything alike.
By the way, I'm using .NET 4.0.
UPDATE: Here's some of my code so you can see what 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()
{
// 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);
}
}