Application State object is destroyed - Dont know why?
-
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(); } }
-
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(); } }
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 -
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(); } }
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()
andApplication.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
-
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 JavascriptAbhishek 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
-
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
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.
-
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.
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
orApp_LocalResources
foldersAdding, 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