We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Rerun Application_Start?

MarcGraff
MarcGraff asked
on
Medium Priority
521 Views
Last Modified: 2012-06-27
How would one programmatically rerun Application_Start?

Thanks!
   - Marc
Comment
Watch Question

Commented:
Its a tough one. Basically, as far as I know, you can't. I think the Application_Start method is kicked off by the inner workings of the HttpApplication object and its factory.

But, there is a better solution in terms of "code design" anyway; this would be to extract your code into a seperate class, and then call that class's methods from Application_Start and any other place you want it called.

Andy

Author

Commented:
Good Morning AGBrown,

My objective is to reload the "Application" variables. I have tryed to create a seporate class but each time it has failed.

My closest attempt:
    Public Sub ReloadVars(ByVal oGlobal As System.Web.HttpApplication)
        oGlobal.Application.Add("TestName", "TestValue")
    End Sub

but I do not know what to pass into this from the "reset" page.

Thanks!
   - Marc
Commented:
Hi Marc,

Try writing your method as:

Public Sub ReloadVars()
      HttpContext.Current.Application.Add("TestName", "TestValue")
End Sub

or

Public Sub ReloadVars()
      HttpContext.Current.Application("TestName") = "TestValue"
End Sub

Note that the "Application" property in Global, or in a web page is simply a wrapper for the HttpContext.Current.Application property, so they are one and the same.

Andy

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Good day AGBrown,
I am working with the code below but it does not retain the Application variables in the global or page context. Any other suggestions?

In Global.asax.vb:
    Public Sub AppReset(ByVal bClearRecSets As Boolean, ByVal bDisplay As Boolean)
        Dim oApp As New App_Reset
        oApp.ReloadVars(bClearRecSets, bDisplay)
    End Sub


In ReloadVars:
Public Class App_Reset
    Public Sub ReloadVars(ByVal bClearRecSets As Boolean, ByVal bDisplay As Boolean)

        HttpContext.Current.Application.Add("Test", "TestValue")
   End Sub
End Class

On ResetPage:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim APP_Vars As New App_Reset
        APP_Vars.ReloadVars(False, False)
    End Sub
Commented:
That's slightly confusing. I have a few ideas to find out what is happening:
1) Put a breakpoint on the line "APP_Vars.ReloadVars(False, False)" in Page_Load. Run your application in debug. When it hits, put a watch on the application object for the key "Test" (i.e. on Application("Test"). Does that value equal "TestValue"?

2) If not, then where does AppReset get called from? Do you have a call to it in the Application_Start method in the global.asax.cs? If you put a breakpoint on the line oApp.ReloadVars(bClearRecSets, bDisplay) and run your application again, does it hit?

3) Put a line of foo code after the call to ReloadVars in AppReset:

Public Sub AppReset(ByVal bClearRecSets As Boolean, ByVal bDisplay As Boolean)
        Dim oApp As New App_Reset
        oApp.ReloadVars(bClearRecSets, bDisplay)
        Dim foo As String = ""
End Sub

Put the breakpoint on Dim foo As String = "", run the application again and put a watch on Application("Test"). Does that value equal "TestValue" now? It should do. If not then something very strange is happening in your application.

Andy

Author

Commented:
Thanks so much Andy for all of your help! And sorry it took me so long to get this up and running!

Commented:
No problem, glad you solved it.

Andy
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.