0

I have a custom membership provider which looks like this:

public class PMembershipProvider : MembershipProvider
{
    public IMembershipService Account { get; set; }

    public PMembershipProvider()
    {
        this.Account = new UserModel();

    }

    public override bool ValidateUser(string username, string password)
    {
        return Account.Login(username, password);
    }

    public void DisposeContext()
    {
        Account.Dispose();
    }
}

And membership service interface which looks like this:

public interface IMembershipService
{
    bool Login(string userName, string password);
    void Dispose();
}

Thus when I go to log in the user I can call the login method of my User Model like so:

public class UserController : Controller
{
    public ActionResult Login(UserModel model)
    {
        if (Membership.ValidateUser(model.name, model.password)) {
            FormsAuthentication.SetAuthCookie(model.name, false, MvcApplication.BASE_URL);
            return RedirectToAction("Index", "Home");
    }
}

I'm running into some strange user issues which I believe are associated with an undisposed EF object context. Based on this answer I'd like to try manually disposing the object context used by the membership provider. I want this to happen at the end of the request, and for that I want to override the Dispose method on my User Controller:

protected override void Dispose(bool disposing)
{
    base.Dispose(disposing);
    Membership.DisposeContext(); //this is what I want to do, but it doesn't work
}

When I try to call the DisposeContext method of my custom membership provider it can't find the method. ('System.Web.Security.Membership' does not contain a definition for 'DisposeContext')

How can I call my DisposeContext method on the same object used by Membership?

Community
  • 1
  • 1
Mansfield
  • 14,445
  • 18
  • 76
  • 112

1 Answers1

2

The Membership class only provides static methods to call the known methods on the default membership provider. To call a custom method, you'll need to access the Membership.Provider property, cast it to your custom provider, and then call the method:

((PMembershipProvider)Membership.Provider).DisposeContext();
Richard Deeming
  • 29,830
  • 10
  • 79
  • 151