3

I can't wrap my head around this issue, where I am assigning a instance to a static class and variable during Appication_Start and the variable become null after a few days.

public static class IocFactory
{
    private static IContainer _appscopeContainer;

    public static void Register(IContainer container = null){
        _appscopeContainer = container
    }

    public static Instance{
        get { return _appscopeContainer; }
    }
}

And I assign an instance to the variable upon Appication_Start, I would expected that the _appscopeContainer presists.

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    Dim appContainer As IContainer = new Container()
    'register framework 4.0 class to container...

    IocFactory.Register(appContainer)
End Sub

On Begin_Request I would build a child container and store it to the HttpContext request cache, and an error will sometimes throw saying that the _appscopeContainer is null after a few days, I have tried recycling the application and it will behivaour normally.

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    Dim requestContainer As IContainer = IocFactory.Instance.BeginLifetimeScope(Tags.RequestLifetimeScopeTag)
End Sub
DavidG
  • 113,891
  • 12
  • 217
  • 223
Franky
  • 91
  • 4
  • 1
    Possible duplicate of http://stackoverflow.com/questions/8919095/lifetime-of-asp-net-static-variable – DavidG Apr 14 '17 at 09:37
  • Where in your project structure is the static class? – DavidG Apr 14 '17 at 09:41
  • Static variables won't be nulled "by themselves". To understand what's going on just implement some persistent logging for the following: 1) static constructor of `IocFactory` 2) Every call to `IocFactory.Register` 3) constructor of application instance 4) first line of `Application_Start`. Then wait for NRE, then analyze. – Lanorkin Apr 14 '17 at 09:53
  • The static class is in a separate dll assembly. But to my understanding dll shouldn't get unloaded right? – Franky Apr 14 '17 at 19:25

0 Answers0