Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Application State object is destroyed - Dont know why?

Application State object is destroyed - Dont know why?

Scheduled Pinned Locked Moved ASP.NET
sysadminhelpquestionannouncement
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vivasaayi
    wrote on last edited by
    #1

    I am running a web service, which provides some updated quotes every 2 minutes. It takes quotes from a quote server. I expected this service to establish a single connection to the quote server. But few times I have notices that this web service establishes more than one connection to the Server. I think that the updateManager.Start() method is called more than one time. But how that could happen? Whether the Application State object is destroyed because of timeout or any internal error? I have a Destructor in UpdateManager Class. Will that be called automatically or I have to call it explicitly? If so how? I welcome your advise and comments. public class Service : System.Web.Services.WebService { UpdateManager updateManager; string key = "update"; public Service() { if (Application[key] != null) { updateManager = (UpdateManager)Application[key]; } else { updateManager = new UpdateManager(); Application[key] = updateManager; //This method will establish a TCP connection with a remote server. //In server, sometimes I am finding more than one TCP connections from this webservice. //Objects placed in the Application State will be persisted till the application ends //so there should be one connection to the server. updateManager.Start(); } } [WebMethod(Description = "Get the Current Quote")] public string GetUpdate() { return updateManager.GetUpdate(); } }

    A N 2 Replies Last reply
    0
    • V vivasaayi

      I am running a web service, which provides some updated quotes every 2 minutes. It takes quotes from a quote server. I expected this service to establish a single connection to the quote server. But few times I have notices that this web service establishes more than one connection to the Server. I think that the updateManager.Start() method is called more than one time. But how that could happen? Whether the Application State object is destroyed because of timeout or any internal error? I have a Destructor in UpdateManager Class. Will that be called automatically or I have to call it explicitly? If so how? I welcome your advise and comments. public class Service : System.Web.Services.WebService { UpdateManager updateManager; string key = "update"; public Service() { if (Application[key] != null) { updateManager = (UpdateManager)Application[key]; } else { updateManager = new UpdateManager(); Application[key] = updateManager; //This method will establish a TCP connection with a remote server. //In server, sometimes I am finding more than one TCP connections from this webservice. //Objects placed in the Application State will be persisted till the application ends //so there should be one connection to the server. updateManager.Start(); } } [WebMethod(Description = "Get the Current Quote")] public string GetUpdate() { return updateManager.GetUpdate(); } }

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      If you are putting the same object in application, I think there shouldnt be any problem. The code looks fine to create one object only which could handle as many session as you want. The only possibility of getting more than one connection is if, a client Requests for GetUpdate and its request is placed just before the Application object is set. I mean, Client 1 requests GetUpdate, which finds that UpdateManager is missing in Application, Meanwhile client 2 makes a request. Client 2 also finds updateManager missing as it is not set to the application object yet. Thus it starts creating another updateManager. The possibility is there for everytime the updateManager object is recreated. ;)

      Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


      My Latest Articles-->** Simplify Code Using NDepend
      Basics of Bing Search API using .NET
      Microsoft Bing MAP using Javascript

      N 1 Reply Last reply
      0
      • V vivasaayi

        I am running a web service, which provides some updated quotes every 2 minutes. It takes quotes from a quote server. I expected this service to establish a single connection to the quote server. But few times I have notices that this web service establishes more than one connection to the Server. I think that the updateManager.Start() method is called more than one time. But how that could happen? Whether the Application State object is destroyed because of timeout or any internal error? I have a Destructor in UpdateManager Class. Will that be called automatically or I have to call it explicitly? If so how? I welcome your advise and comments. public class Service : System.Web.Services.WebService { UpdateManager updateManager; string key = "update"; public Service() { if (Application[key] != null) { updateManager = (UpdateManager)Application[key]; } else { updateManager = new UpdateManager(); Application[key] = updateManager; //This method will establish a TCP connection with a remote server. //In server, sometimes I am finding more than one TCP connections from this webservice. //Objects placed in the Application State will be persisted till the application ends //so there should be one connection to the server. updateManager.Start(); } } [WebMethod(Description = "Get the Current Quote")] public string GetUpdate() { return updateManager.GetUpdate(); } }

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        Problem here is thread safety. Your code is not thread safe and that is the reason for all weird results. Take look at Application.Lock() and Application.UnLock() methods. These methods are designed to provide thread safety when accessing/writing values into application object. Having said that, I don't think keeping a single instance of connection in application variable is a good idea. Open the TCP connection each time and closing when done would be ideal solution. If you think it has more overhead, then store it in cache rather than keeping in application state.

        Best wishes, Navaneeth

        1 Reply Last reply
        0
        • A Abhishek Sur

          If you are putting the same object in application, I think there shouldnt be any problem. The code looks fine to create one object only which could handle as many session as you want. The only possibility of getting more than one connection is if, a client Requests for GetUpdate and its request is placed just before the Application object is set. I mean, Client 1 requests GetUpdate, which finds that UpdateManager is missing in Application, Meanwhile client 2 makes a request. Client 2 also finds updateManager missing as it is not set to the application object yet. Thus it starts creating another updateManager. The possibility is there for everytime the updateManager object is recreated. ;)

          Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


          My Latest Articles-->** Simplify Code Using NDepend
          Basics of Bing Search API using .NET
          Microsoft Bing MAP using Javascript

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          Abhishek Sur wrote:

          The code looks fine to create one object only which could handle as many session as you want.

          No it doesn't. It looks horrible in fact. :)

          Best wishes, Navaneeth

          V 1 Reply Last reply
          0
          • N N a v a n e e t h

            Abhishek Sur wrote:

            The code looks fine to create one object only which could handle as many session as you want.

            No it doesn't. It looks horrible in fact. :)

            Best wishes, Navaneeth

            V Offline
            V Offline
            vivasaayi
            wrote on last edited by
            #5

            Thank you for your comments. I am sure that no clients are connected when the application starts up. And I dont want to implement Application.Lock() because I dont want the clients to wait and also the clients will not modify the updateManagerObject in the Application State. I tested the UpdateManager class with Windows Forms and found that the Receiver Thread in UpdateManager class(Which downloads data from the Server) is orphaned sometimes. I simulated the same in the Web Service Also.

            [WebMethod(Description = "Get the Current Quote")]
            public string GetUpdate()
            {
            //The following code re-initializes the updateManagerObject. So the Receiver
            //Thread in the old updateManagerObject becomes Orphan.
            updateManager = new UpdateManager();
            Application[key] = cache;
            updateManager.Start();

            return updateManager.GetUpdate();	
            

            }

            This happens whenever the updateManager is re-initialized without destroying the old object. If the previous object is destroyed properly (by calling Dispose method) then everything works fine and no orphan Threads. As you can see from the Web Service Constructor, the updateManager object is initialized when that object is null. Other wise it takes from the Application Object. My question is How an object which is initialized and Stored in Application object becomes null? Since it becomes null the UpdateManager Constructor is called once again. How can I cal the Dispose() method? I have no way to find the orphan threads, to destroy them. Please suggest me some solutions.

            A 1 Reply Last reply
            0
            • V vivasaayi

              Thank you for your comments. I am sure that no clients are connected when the application starts up. And I dont want to implement Application.Lock() because I dont want the clients to wait and also the clients will not modify the updateManagerObject in the Application State. I tested the UpdateManager class with Windows Forms and found that the Receiver Thread in UpdateManager class(Which downloads data from the Server) is orphaned sometimes. I simulated the same in the Web Service Also.

              [WebMethod(Description = "Get the Current Quote")]
              public string GetUpdate()
              {
              //The following code re-initializes the updateManagerObject. So the Receiver
              //Thread in the old updateManagerObject becomes Orphan.
              updateManager = new UpdateManager();
              Application[key] = cache;
              updateManager.Start();

              return updateManager.GetUpdate();	
              

              }

              This happens whenever the updateManager is re-initialized without destroying the old object. If the previous object is destroyed properly (by calling Dispose method) then everything works fine and no orphan Threads. As you can see from the Web Service Constructor, the updateManager object is initialized when that object is null. Other wise it takes from the Application Object. My question is How an object which is initialized and Stored in Application object becomes null? Since it becomes null the UpdateManager Constructor is called once again. How can I cal the Dispose() method? I have no way to find the orphan threads, to destroy them. Please suggest me some solutions.

              A Offline
              A Offline
              Abhishek Sur
              wrote on last edited by
              #6

              As far as I know, Application object is reinitialized only if : ASP.NET source is modified, which makes the ASP.NET to recompile,

              # Adding, modifying, or deleting assemblies from the application's Bin folder

              Adding, modifying, or deleting localization resources from the App_GlobalResources or App_LocalResources folders

              Adding, modifying, or deleting the application's Global.asax file.

              Adding, modifying, or deleting source code files in the App_Code directory.

              Adding, modifying, or deleting Profile configuration.

              Adding, modifying, or deleting Web service references in the App_WebReferences directory.

              Adding, modifying, or deleting the application's Web.config file.

              Check this : http://msdn.microsoft.com/en-us/library/ms178473.aspx[^] :rose:

              Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


              My Latest Articles-->** Simplify Code Using NDepend
              Basics of Bing Search API using .NET
              Microsoft Bing MAP using Javascript

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups