0

I am trying to understand an execution sequence in a web application environment - basically I am using a module for various utility functions, including logging and initializing various global/shared variables. The website has several stand-alone pages and a few web-services.

We've had some issues with the website where one of the developers suggested that a piece of code that has to be executed first to initialize a crucial property (which happens in a module constructor, the property itself is NOT defined in a module, but rather on a 3rd party DLL and module simply initiates it in a constructor) is not being executed first. The reasoning behind that developer is "The module's constructor is only being called when a reference to it is found on a page that was opened.

So lets imagine there are 2 pages on our web application. Only Page2 has a reference to the MyModule. So the logic of this developer is, if the web site is restarted and the very first call is to Page1, which does not have any references to the module and therefore constructor is not called and therefore it does not initialize this property on a 3rd party DLL. Because of that, Page1 tries to use the 3rd party DLL with uninitialized property and it fails.

My counter to that is: Module is a shared class and its constructor, which is also shared is being initialized when the web application's assembly is loaded. According to my argument, it doesn't matter which page gets loaded first, constructor initiates the 3rd party tool when assembly loads and therefore the issue is somewhere else.

Am I right or am I wrong? I couldn't find the answer to my question, just some info on how Module is similar to C# static classes and yet its different.

Thank you

Public Module MyModule
    Sub New()
        Log("something")
        myThirdPartyDLLReference.InitializeVars()
        ...
    End Sub

Private Sub Log(ByVal logMessage as string)
    ' Do some logic. Log some information
End Sub 

End Module

Partial Class Page1
    Inherits Page

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        ' Do some logic, no references to MyModule
    End Sub
End Class

Partial Class Page2
    Inherits Page

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        ' Do some logic, initialize MyModule
        MyModule.Log("Page 2 initialized")
    End Sub
End Class
George
  • 2,165
  • 2
  • 22
  • 34

1 Answers1

3

Your developer is right. Modules are only loaded when needed because the module list is empty when the program starts https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.modules?view=netframework-4.7.2

this is also my experince and is shown in your experience also

nbk
  • 45,398
  • 8
  • 30
  • 47
  • I believe you are correct. I created a dummy module and 2 dummy pages. Page1 writes into file directly, Page2 calls the module's function that writes into file (constructor also writes into file on load). If I call Page1, only one line is written into file. If I call Page2, 2 lines are written into file - one from constructor and one from the page. – George Apr 15 '19 at 14:53
  • Followup question: is there a way to enforce loading the module no matter what page is accessed first (even if it doesn't reference the module)? – George Apr 15 '19 at 15:56
  • create a public variable, function or sub in your module and access it on every page ´, it is certain that new was called once. – nbk Apr 15 '19 at 17:24
  • I already have that. Unfortunately I cannot control a situation where someone creates a new page and does not reference that function/sub. So is there a way to enforce loading of the module regardless of the page loaded and whether that page has any reference to the module or not? – George Apr 16 '19 at 18:06
  • you could define a new method for every created page, you inherit everything from the base class page, so you are free to extend or overwrite anything you get from page – nbk Apr 16 '19 at 19:32
  • We already have a base class page, that I cannot change. If I create another method that a new page can inherit from, how do I enforce this condition if I am not the one creating the new page? If another developer comes into the project after I am done with it and they create a new page, there is no guarantee they will use the method I created (unless I overwrite the base page, which I cannot do) – George Apr 16 '19 at 20:24