Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am trying to add an array list to a cache object in my WebService. But it only loads the first 2 attempts and ignores any other attempts to load data into the cache.

 

Why?

 

Imports System.Web.Services
'Imports System.Web.Caching.Cache

<System.Web.Services.WebService(Namespace:="http://tempuri.org/wsTranscriptsTest/Service1")> _
Public Class HoldingTank
   Inherits System.Web.Services.WebService
   Dim strArray As New ArrayList

#Region " Web Services Designer Generated Code "

   Public Sub New()
       MyBase.New()

       'This call is required by the Web Services Designer.
       InitializeComponent()

       'Add your own initialization code after the InitializeComponent() call

   End Sub

   'Required by the Web Services Designer
   Private components As System.ComponentModel.IContainer

   'NOTE: The following procedure is required by the Web Services Designer
   'It can be modified using the Web Services Designer.  
   'Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       components = New System.ComponentModel.Container
   End Sub

   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       'CODEGEN: This procedure is required by the Web Services Designer
       'Do not modify it using the code editor.
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub

#End Region

[color=green]
   <WebMethod()> _
   Public Function HelloWorld() As ArrayList
       If Context.Cache("strArray") Is Nothing Then
           CreateCache()
       End If
       Dim arrReturnList As ArrayList = CType(Context.Cache("strArray"), ArrayList)
       Context.Cache.Remove("strArray")
       Return arrReturnList
   End Function

   <WebMethod()> _
  Public Sub FillCache(ByVal strValue As String)
       If Context.Cache("strArray") Is Nothing Then
           CreateCache()
       End If
       strArray.Add(strValue)
       Context.Cache.Add("strArray", strArray, Nothing, Now.AddHours(1), Nothing, Caching.CacheItemPriority.High, Nothing)
   End Sub

   Private Sub CreateCache()
       strArray.Add("StarterValue")
       'Context.Cache.Insert("strArray", strArray)
       Context.Cache.Add("strArray", strArray, Nothing, Now.AddHours(1), Nothing, Caching.CacheItemPriority.High, Nothing)
   End Sub
End Class
[/color]

Alex Polajenko
Posted

I seemed to figure it out. Still don't CLEARLY understand why though ;)

 

<WebMethod()> _

Public Function HelloWorld() As ArrayList

If Context.Cache("strArray") Is Nothing Then

CreateCache()

End If

Dim arrReturnList As ArrayList = CType(Context.Cache("strArray"), ArrayList)

Context.Cache.Remove("strArray")

Return arrReturnList

End Function

 

<WebMethod()> _

Public Sub FillCache(ByVal strValue As String)

If Context.Cache("strArray") Is Nothing Then

CreateCache()

End If

strArray = CType(Context.Cache("strArray"), ArrayList)

strArray.Add(strValue)

Context.Cache.Add("strArray", strArray, Nothing, Now.AddHours(1), Nothing, Caching.CacheItemPriority.High, Nothing)

End Sub

 

Private Sub CreateCache()

strArray.Add("StarterValue")

Context.Cache.Add("strArray", strArray, Nothing, Now.AddHours(1), Nothing, Caching.CacheItemPriority.High, Nothing)

End Sub

Alex Polajenko
  • *Experts*
Posted
If you have Option Strict on, then you need to cast objects to their correct types before using them. For example,
Context.Cache("strArray")

actually returns an Object, which you need to cast to ArrayList before it is usable.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...