Salve a tutti,

ho un problema nella gestione dei task schedulati tramite l'utilizzo dell'oggetto HttpContext.Current.Cache.

In sostanza ho l'esigenza di creare uno scheduler ovvero un processo reiterato senza l'ausilio dell'intervento di un operatore umano ...per chi conosce java, l'analogo di un TimerTask.

Spulciando tra i forum e materiale vario ho visto che in asp.net è possibile anche se tramite un trucco, ovvero sfruttando le proprietà dell'oggetto Cache.

ho realizzato il task all'interno del file global.asax, avviandolo all'interno del metodo Application_OnStart()

La procedura che ho realizzato funziona correttamente ...il problema è che dopo un pò di tempo il task si interrompe, anche se in realtà è stato codificato per ripetersi per un tempo indefinito.

Ho cercato di capire da cosa potesse dipendere ma non riesco ad arrivare ad una soluzione.

Allego al post tutto il codice del file global.asax che ho realizzato.

Spero che qualcuno possa darmi qualche indicazione ...almeno da capire se è un errore mio o se è un problema di altra natura, legato all'oggetto Cache o ad altri oggetti accessori che utilizzo.

nello specifico, il task in questione deve chiamare ciclicamente, ogni ora una pagina che aggiorna una tabella di valute, la chiamata oraria è ottenuta tramite quest'impostazione New TimeSpan(1, 0, 0)

(mi è venuto il dubbio sull'oggetto TimeSpan che utilizzo per definire i cicli di ripetizione, ma dai vari controlli mi sembra usato correttamente).

Grazie in anticipo


FILE global.asax:


<%@ Language="VB" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Caching" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Io" %>


<script runat=server>

Dim CURRENCY_CACHE_KEY As String = "CurrencyCacheKey"

Dim onRemoveCurrency As CacheItemRemovedCallback

Dim configFileName As String = ""


Sub Application_OnStart()
Application("TotalUsers") = 1

Application(CURRENCY_CACHE_KEY) = HttpContext.Current.Cache
RegisterCurrencyCacheEntry()

configFileName = HttpContext.Current.Server.MapPath("/public/conf/nemesi_config.xml")
End Sub

Sub Application_OnEnd()
End Sub


Sub Session_OnStart()
Application("SessionStarts") = Application("SessionStarts") + 1
Session.TimeOut = 1
Application.Unlock
End Sub

Sub Session_OnEnd()
End Sub


'***************************************** METODI PRIVATI PER LA GESTIONE DEL TASK CURRENCY ****************************

Private Sub RegisterCurrencyCacheEntry()
Dim objCache
objCache = Application(CURRENCY_CACHE_KEY)
onRemoveCurrency = New CacheItemRemovedCallback(AddressOf CurrencyRemovedCallback)
objCache.Add("currency", "currency val", Nothing, DateTime.MaxValue, New TimeSpan(1, 0, 0), System.Web.Caching.CacheItemPriority.Default, onRemoveCurrency)
End Sub

Private Sub CurrencyRemovedCallback(ByVal key As String, ByVal value As Object, ByVal removedReason As CacheItemRemovedReason)
CurrencyServiceActions()
RegisterCurrencyCacheEntry()
End Sub

Private Sub CurrencyServiceActions()
Dim td As New Thread(AddressOf DoCurrencyServiceActions)
td.Start()
End Sub

Private Sub DoCurrencyServiceActions()

'*********************************** QUI GESTISCO LE CHIAMATE RICORSIVE VERSO IL SERVIZIO DI VALUTE

Try

' imposto il servername, se già presente, lo recupero dall'oggetto application
' in caso contrario lo estraggo da un file xml di configurazione

Dim serverName As String = "localhost"

if not(Application("currencysrvname") = "") then
serverName = Application("currencysrvname")
else

'Get a StreamReader class that can be used to read the file
Dim objStreamReader as StreamReader
objStreamReader = File.OpenText(configFileName)

'Now, read the entire file into a string
Dim contents as String = objStreamReader.ReadToEnd()

Dim m_xmld As XmlDocument
Dim m_node As XmlNode

'Create the XML Document
m_xmld = New XmlDocument()

'Load the Xml file
m_xmld.Load(new StringReader(contents))

'Get the servername of the xml file
m_node = m_xmld.SelectSingleNode("/config/srt_default_server_name")

serverName = m_node.Attributes.GetNamedItem("attr_srt_default_s erver_name").Value.toString()
end if

Application("currencysrvname") = serverName

Dim uriString As String = "http://"+serverName+"/editor/currency/currencyPoller.asp"

Dim uri As New Uri(uriString)

Dim request as HttpWebRequest = HttpWebRequest.Create(uri)
request.Method = WebRequestMethods.Http.Get

Dim response As HttpWebResponse = request.GetResponse()
Dim reader As New StreamReader(response.GetResponseStream())
Dim tmp As String = reader.ReadToEnd()
response.Close()

Catch errorVariable As Exception
'Error trapping
Console.Write(errorVariable.ToString())
Application("error") = errorVariable.ToString()
End Try

End Sub


</SCRIPT>