Shared Properties disappearing

S

Simon

Hi all,

We have an ASP.NET 1.1 application running on IIS6 on Server 2003.

Most of the base objects we are using in this application are taken from a
windows application also written by us. We have used shared properties on
some of these objects to enable caching of frequently used objects, so save
fetching them from SQL every time. These shared properties vastly increase
performance of our windows app.

So in our web app, we have left the same caching technique, by using shared
properties we cache various objects, usually in collections.

These collections have been strongly typed by utilising seperate classes and
storing an internal collection, e.g. see below.

Public Class AttributeCollection

Implements IEnumerable

Protected intCollection As New Collection

Public Overridable Sub Add(ByVal Attr As Attribute)
intCollection.Add(Attr, Attr.Name)
End Sub

Public Sub Remove(ByVal Attr As Attribute)
intCollection.Remove(Attr.Name)
End Sub

Public Sub Remove(ByVal AttrName As String)
intCollection.Remove(AttrName)
End Sub

Public Function Count() As Integer
Return intCollection.Count
End Function

Default ReadOnly Property Item(ByVal AttrName As String) As Attribute
Get
Return CType(intCollection.Item(AttrName), Attribute)
End Get
End Property
Default ReadOnly Property Item(ByVal index As Integer) As Attribute
Get
Return CType(intCollection.Item(index), Attribute)
End Get
End Property

Public Function GetEnumerator() As System.Collections.IEnumerator
Implements System.Collections.IEnumerable.GetEnumerator
Return intCollection.GetEnumerator
End Function


End Class

The problem, is that at certain times, the cached collection returns
containing nothing in the internal collection.

So the shared Property is returning ok, and is pointing to one of the
collections as shown above, however, the intcollection in the above code
contains nothing. so the code falls over. We cannot explain why the internal
collection for these objects is being emptied?

Anyone any ideas?

Thanks in advance and regards,
Simon.
 
S

Simon

Thanks for the reply Milosz,

I understand your reasoning, but if all shared variables are lost, how come
the reference to the shared collection is working, it is only when it tries
to access the intCollection inside the shared collection class that the
application falls over.

e.g. Taking the collection from below, and putting it in a shared property,
Which is how it is working in our app.

Public Class Attribute

Private Shared _attributes As AttributeCollection

Public Shared Sub RefreshCache()

' Code to build the collection in here....

End Sub

Public Shared ReadOnly Property Attributes() As AttributeCollection
Get
If _attributes Is Nothing Then
RefreshCache()
End If
Return _attributes
End Get

End Property

End Class

Public Class AttributeCollection

Implements IEnumerable

Protected intCollection As New Collection

Public Overridable Sub Add(ByVal Attr As Attribute)
intCollection.Add(Attr, Attr.Name)
End Sub

Public Sub Remove(ByVal Attr As Attribute)
intCollection.Remove(Attr.Name)
End Sub

Public Sub Remove(ByVal AttrName As String)
intCollection.Remove(AttrName)
End Sub

Public Function Count() As Integer
Return intCollection.Count
End Function

Default ReadOnly Property Item(ByVal AttrName As String) As Attribute
Get
Return CType(intCollection.Item(AttrName), Attribute)
End Get
End Property
Default ReadOnly Property Item(ByVal index As Integer) As Attribute
Get
Return CType(intCollection.Item(index), Attribute)
End Get
End Property

Public Function GetEnumerator() As System.Collections.IEnumerator
Implements System.Collections.IEnumerable.GetEnumerator
Return intCollection.GetEnumerator
End Function

Public Function Contains(ByVal attrName As String) As Boolean
For Each attr As Attribute In intCollection
If attr.Name.ToLower = attrName.ToLower Then
Return True
End If
Next
Return False
End Function

End Class


Any code that references Attribute.Attributes never returns a null
reference, it always appears to return the correct collection object. But
when you then use Attribute.Attributes.Contains("name") it falls over in the
contains code. Saying that intcollection is nothing.

Hope you understand more.
And thanks again for your reply.
Simon.
 
G

Guest

Hi again,

First of all, your code is not thread safe. If you're going to use
static/shared members in multithreading environment (as in ASP.NET) you MUST
make it thread safe (synchronization, locks, etc). Use thread safe singleton
class or what’s quicker, save the time by simply using Application State or
Cache classes that are designed for storing/caching frequently used data in
such environment.

Hope this helps
 
S

Simon

Hi Milosz,

Thanks again for taking the time to reply.

So do you think that is the problem, that the classes are not thread safe,
and mutliple thread are possibly writing to the shared properties? and
somehow causing the internal intcollection to be nothing. It sounds
unlikely, but we're clutching at straws here, so are willing to try
anything.

So we wish to ensure that our objects are thread safe, however, these
objects are used in both a windows and an ASP.NET environment.
How is it best to go about making a class that is used in both asp.net and
windows thread safe for both environments?

Regards,
Simon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top