0

We get a System.ObjectDisposedException: Cannot access a disposed object exception in the 'DeactivateUser' method shown below. What's wrong with this code?

public class HomeController
{
    private readonly IUserService _userService;

    public HomeController(IUserService userService)
    {
        _userService= userService;
    }

    public void DeactivateUser(int id)
    {
        try
        {
            _userService.Deactivate(id);
        }
        catch (Exception e)
        {
            //Log error
            throw;
        }
    }
}

The code that calls it from the Razor View is as follows:

function Deactivate() {   
$.ajax({
    url: "/Home/DeactivateUser?id="+$("#Id").val(),
    async: false,
    type: 'POST',
    cache: false,
    success: function (result) {
        alert("Deactivated User");
    }
});

}

It is to be noted that the Deactivate ajax method gets called at a specific interval controlled by a timer.

It is MVC 4 and yes, it is registered as a singleton in our dependency injection container.

user2893547
  • 135
  • 1
  • 9
  • Is this MVC 6/ Core 1? If so, how are you registering the IUserService in Startup.cs ? – user1142433 Apr 26 '16 at 01:13
  • It doesn't look like there's anything wrong with the snippet you've provided, but the most likely explanation is that something else in your application also has a reference to the `IUserService` and has disposed of it. Is this registered as a singleton in your dependency injection container? – yaakov Apr 26 '16 at 01:53
  • I mean the best course of action here is to run this in debug and see which object is actually being disposed before it is passed into this constructor. – Tdorno Apr 26 '16 at 14:21
  • Show us the code of the `IUserService` implementation, please. – Maarten Apr 26 '16 at 14:56

0 Answers0