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);
}
}
I just blogged about this with a solution.
In a nutshell, this solution involves a simple, reusable MembershipProvider that calls the container to resolve your custom MembershipProviders. Unlike other solutions that use "BuildUp" container features, this one takes true control of instantiation, thus enabling constructor injection (which in turn enables immutability) and proxyability.
Don't worry about your MembershipProvider lifetime. Simply manage the lifetime of the IMyService within the provider. Create a property for your IMyService with a getter, and return a new instance (or however you want to manage the lifetime) each time it is requested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With