Question

I have two projects in my solution: BusinessLibrary and Web (asp.net MVC 4). I'm using autofac for DI and it seems that after some inactivity, the injected object in the controller becomes null. Am I doing something wrong the lifecycle configuration in autofac?

Web project

AccountController references a service class from BusinessLibrary like this:

public class AccountController : Controller
{
    private readonly IAccountService accountService;

    public AccountController(){}

    public AccountController(IAccountService accountService)
    {
        this.accountService = accountService;
    }

    [HttpPost]
    public ActionResult Logon(User model)
    {
        if (accountService.AuthenticateUser(model.Username, model.Password))
        {
            //... some logic here
        }
    }
}

This is Global.asax.cs:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        //some other logic here
        IoCConfig.RegisterDependencies();
    }
}

Finally, autofac configuration in IoCConfig:

    public static void RegisterDependencies()
    {
        var builder = new ContainerBuilder();

        // register services
        builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
            .Where(t => t.Name.EndsWith("Service"))
            .AsImplementedInterfaces()
            .InstancePerHttpRequest();

        // register controllers
        builder.RegisterControllers(typeof(MvcApplication).Assembly).InstancePerHttpRequest();

        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    }

BusinessLibrary project

AccountService class:

public interface IAccountService
{
    bool AuthenticateUser(string username, string password);
}

public class AccountService : IAccountService
{
    private readonly IAuthenticationService authenticationService;

    public AccountService(IAuthenticationService authenticationService)
    {
        this.authenticationService = authenticationService;
    }

    public bool AuthenticateUser(string username, string password)
    {
        //some logic that uses authenticationService
    }
}

AccountService accepts an instance of IAuthenticationService which I get from a referenced dll, which does some AD authentication logic.

Problem

When I run it on my local machine (IIS 7.5) after rebuilding in VS2012, I am able to login and everything works fine. Now, if I leave the tab open and do something else, after I come page to the page and try to do any action, I get an exception: Object reference not set to an instance of an object. The accountService is null in the controller here:

public class AccountController : Controller
{
    // constructors...
    [HttpPost]
    public ActionResult Logon(User model)
    {
        if (accountService.AuthenticateUser(model.Username, model.Password)) // accountService is null

Any help would be greatly appreciated.

PS. I checked similar problems on SO, but those were around private constructors or double autofac configuration in web.config files.

Was it helpful?

Solution

According to your last comment, your web project need to reference all Service Projects.

Basically, you should be able to register like this -

builder.RegisterType<AccountService>().As<IAccountService>()
   .InstancePerHttpReq‌​uest();
builder.RegisterType<AuthenticationService>().As<IAuthenticationService>()
   .InstancePerHttpReq‌​uest();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top