Shared Properties disappearing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon

    Shared Properties disappearing

    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 AttributeCollec tion

    Implements IEnumerable

    Protected intCollection As New Collection

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

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

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

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

    Default ReadOnly Property Item(ByVal AttrName As String) As Attribute
    Get
    Return CType(intCollec tion.Item(AttrN ame), Attribute)
    End Get
    End Property
    Default ReadOnly Property Item(ByVal index As Integer) As Attribute
    Get
    Return CType(intCollec tion.Item(index ), Attribute)
    End Get
    End Property

    Public Function GetEnumerator() As System.Collecti ons.IEnumerator
    Implements System.Collecti ons.IEnumerable .GetEnumerator
    Return intCollection.G etEnumerator
    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.



  • Milosz Skalecki

    #2
    RE: Shared Properties disappearing

    Hi there,

    This is because web application is being unloaded from appdomain and all
    shared variables are lost. Try to not use static memebrs in ASP.NET
    applications, use Application state + Application_Sta rt/Application_End
    events instead.

    Please find detailed explanation of this problem here

    --
    Milosz Skalecki
    MCP, MCAD


    "Simon" wrote:
    [color=blue]
    > 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 AttributeCollec tion
    >
    > Implements IEnumerable
    >
    > Protected intCollection As New Collection
    >
    > Public Overridable Sub Add(ByVal Attr As Attribute)
    > intCollection.A dd(Attr, Attr.Name)
    > End Sub
    >
    > Public Sub Remove(ByVal Attr As Attribute)
    > intCollection.R emove(Attr.Name )
    > End Sub
    >
    > Public Sub Remove(ByVal AttrName As String)
    > intCollection.R emove(AttrName)
    > End Sub
    >
    > Public Function Count() As Integer
    > Return intCollection.C ount
    > End Function
    >
    > Default ReadOnly Property Item(ByVal AttrName As String) As Attribute
    > Get
    > Return CType(intCollec tion.Item(AttrN ame), Attribute)
    > End Get
    > End Property
    > Default ReadOnly Property Item(ByVal index As Integer) As Attribute
    > Get
    > Return CType(intCollec tion.Item(index ), Attribute)
    > End Get
    > End Property
    >
    > Public Function GetEnumerator() As System.Collecti ons.IEnumerator
    > Implements System.Collecti ons.IEnumerable .GetEnumerator
    > Return intCollection.G etEnumerator
    > 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.
    >
    >
    >
    >[/color]

    Comment

    • Simon

      #3
      Re: Shared Properties disappearing

      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 AttributeCollec tion

      Public Shared Sub RefreshCache()

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

      End Sub

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

      End Property

      End Class

      Public Class AttributeCollec tion

      Implements IEnumerable

      Protected intCollection As New Collection

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

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

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

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

      Default ReadOnly Property Item(ByVal AttrName As String) As Attribute
      Get
      Return CType(intCollec tion.Item(AttrN ame), Attribute)
      End Get
      End Property
      Default ReadOnly Property Item(ByVal index As Integer) As Attribute
      Get
      Return CType(intCollec tion.Item(index ), Attribute)
      End Get
      End Property

      Public Function GetEnumerator() As System.Collecti ons.IEnumerator
      Implements System.Collecti ons.IEnumerable .GetEnumerator
      Return intCollection.G etEnumerator
      End Function

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

      End Class


      Any code that references Attribute.Attri butes never returns a null
      reference, it always appears to return the correct collection object. But
      when you then use Attribute.Attri butes.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.


      "Milosz Skalecki" <mily242@REMOVE ITwp.pl> wrote in message
      news:EE0E0F5D-1CF3-4B76-B7F3-30518EDB7695@mi crosoft.com...[color=blue]
      > Hi there,
      >
      > This is because web application is being unloaded from appdomain and all
      > shared variables are lost. Try to not use static memebrs in ASP.NET
      > applications, use Application state + Application_Sta rt/Application_End
      > events instead.
      >
      > Please find detailed explanation of this problem here
      > http://www.devsource.com/article2/0,1895,1876120,00.asp
      > --
      > Milosz Skalecki
      > MCP, MCAD
      >
      >
      > "Simon" wrote:
      >[color=green]
      >> 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 AttributeCollec tion
      >>
      >> Implements IEnumerable
      >>
      >> Protected intCollection As New Collection
      >>
      >> Public Overridable Sub Add(ByVal Attr As Attribute)
      >> intCollection.A dd(Attr, Attr.Name)
      >> End Sub
      >>
      >> Public Sub Remove(ByVal Attr As Attribute)
      >> intCollection.R emove(Attr.Name )
      >> End Sub
      >>
      >> Public Sub Remove(ByVal AttrName As String)
      >> intCollection.R emove(AttrName)
      >> End Sub
      >>
      >> Public Function Count() As Integer
      >> Return intCollection.C ount
      >> End Function
      >>
      >> Default ReadOnly Property Item(ByVal AttrName As String) As Attribute
      >> Get
      >> Return CType(intCollec tion.Item(AttrN ame), Attribute)
      >> End Get
      >> End Property
      >> Default ReadOnly Property Item(ByVal index As Integer) As Attribute
      >> Get
      >> Return CType(intCollec tion.Item(index ), Attribute)
      >> End Get
      >> End Property
      >>
      >> Public Function GetEnumerator() As System.Collecti ons.IEnumerator
      >> Implements System.Collecti ons.IEnumerable .GetEnumerator
      >> Return intCollection.G etEnumerator
      >> 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.
      >>
      >>
      >>
      >>[/color][/color]


      Comment

      • Milosz Skalecki

        #4
        Re: Shared Properties disappearing

        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 (synchronizatio n, 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
        --
        Milosz Skalecki
        MCP, MCAD


        "Simon" wrote:
        [color=blue]
        > 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 AttributeCollec tion
        >
        > Public Shared Sub RefreshCache()
        >
        > ' Code to build the collection in here....
        >
        > End Sub
        >
        > Public Shared ReadOnly Property Attributes() As AttributeCollec tion
        > Get
        > If _attributes Is Nothing Then
        > RefreshCache()
        > End If
        > Return _attributes
        > End Get
        >
        > End Property
        >
        > End Class
        >
        > Public Class AttributeCollec tion
        >
        > Implements IEnumerable
        >
        > Protected intCollection As New Collection
        >
        > Public Overridable Sub Add(ByVal Attr As Attribute)
        > intCollection.A dd(Attr, Attr.Name)
        > End Sub
        >
        > Public Sub Remove(ByVal Attr As Attribute)
        > intCollection.R emove(Attr.Name )
        > End Sub
        >
        > Public Sub Remove(ByVal AttrName As String)
        > intCollection.R emove(AttrName)
        > End Sub
        >
        > Public Function Count() As Integer
        > Return intCollection.C ount
        > End Function
        >
        > Default ReadOnly Property Item(ByVal AttrName As String) As Attribute
        > Get
        > Return CType(intCollec tion.Item(AttrN ame), Attribute)
        > End Get
        > End Property
        > Default ReadOnly Property Item(ByVal index As Integer) As Attribute
        > Get
        > Return CType(intCollec tion.Item(index ), Attribute)
        > End Get
        > End Property
        >
        > Public Function GetEnumerator() As System.Collecti ons.IEnumerator
        > Implements System.Collecti ons.IEnumerable .GetEnumerator
        > Return intCollection.G etEnumerator
        > End Function
        >
        > Public Function Contains(ByVal attrName As String) As Boolean
        > For Each attr As Attribute In intCollection
        > If attr.Name.ToLow er = attrName.ToLowe r Then
        > Return True
        > End If
        > Next
        > Return False
        > End Function
        >
        > End Class
        >
        >
        > Any code that references Attribute.Attri butes never returns a null
        > reference, it always appears to return the correct collection object. But
        > when you then use Attribute.Attri butes.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.
        >
        >
        > "Milosz Skalecki" <mily242@REMOVE ITwp.pl> wrote in message
        > news:EE0E0F5D-1CF3-4B76-B7F3-30518EDB7695@mi crosoft.com...[color=green]
        > > Hi there,
        > >
        > > This is because web application is being unloaded from appdomain and all
        > > shared variables are lost. Try to not use static memebrs in ASP.NET
        > > applications, use Application state + Application_Sta rt/Application_End
        > > events instead.
        > >
        > > Please find detailed explanation of this problem here
        > > http://www.devsource.com/article2/0,1895,1876120,00.asp
        > > --
        > > Milosz Skalecki
        > > MCP, MCAD
        > >
        > >
        > > "Simon" wrote:
        > >[color=darkred]
        > >> 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 AttributeCollec tion
        > >>
        > >> Implements IEnumerable
        > >>
        > >> Protected intCollection As New Collection
        > >>
        > >> Public Overridable Sub Add(ByVal Attr As Attribute)
        > >> intCollection.A dd(Attr, Attr.Name)
        > >> End Sub
        > >>
        > >> Public Sub Remove(ByVal Attr As Attribute)
        > >> intCollection.R emove(Attr.Name )
        > >> End Sub
        > >>
        > >> Public Sub Remove(ByVal AttrName As String)
        > >> intCollection.R emove(AttrName)
        > >> End Sub
        > >>
        > >> Public Function Count() As Integer
        > >> Return intCollection.C ount
        > >> End Function
        > >>
        > >> Default ReadOnly Property Item(ByVal AttrName As String) As Attribute
        > >> Get
        > >> Return CType(intCollec tion.Item(AttrN ame), Attribute)
        > >> End Get
        > >> End Property
        > >> Default ReadOnly Property Item(ByVal index As Integer) As Attribute
        > >> Get
        > >> Return CType(intCollec tion.Item(index ), Attribute)
        > >> End Get
        > >> End Property
        > >>
        > >> Public Function GetEnumerator() As System.Collecti ons.IEnumerator
        > >> Implements System.Collecti ons.IEnumerable .GetEnumerator
        > >> Return intCollection.G etEnumerator
        > >> 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.
        > >>
        > >>
        > >>
        > >>[/color][/color]
        >
        >
        >[/color]

        Comment

        • Simon

          #5
          Re: Shared Properties disappearing

          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.

          "Milosz Skalecki" <mily242@REMOVE ITwp.pl> wrote in message
          news:E856044A-2701-49D9-A31A-E06670ABA734@mi crosoft.com...[color=blue]
          > 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 (synchronizatio n, 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
          > --
          > Milosz Skalecki
          > MCP, MCAD
          >
          >
          > "Simon" wrote:
          >[color=green]
          >> 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 AttributeCollec tion
          >>
          >> Public Shared Sub RefreshCache()
          >>
          >> ' Code to build the collection in here....
          >>
          >> End Sub
          >>
          >> Public Shared ReadOnly Property Attributes() As AttributeCollec tion
          >> Get
          >> If _attributes Is Nothing Then
          >> RefreshCache()
          >> End If
          >> Return _attributes
          >> End Get
          >>
          >> End Property
          >>
          >> End Class
          >>
          >> Public Class AttributeCollec tion
          >>
          >> Implements IEnumerable
          >>
          >> Protected intCollection As New Collection
          >>
          >> Public Overridable Sub Add(ByVal Attr As Attribute)
          >> intCollection.A dd(Attr, Attr.Name)
          >> End Sub
          >>
          >> Public Sub Remove(ByVal Attr As Attribute)
          >> intCollection.R emove(Attr.Name )
          >> End Sub
          >>
          >> Public Sub Remove(ByVal AttrName As String)
          >> intCollection.R emove(AttrName)
          >> End Sub
          >>
          >> Public Function Count() As Integer
          >> Return intCollection.C ount
          >> End Function
          >>
          >> Default ReadOnly Property Item(ByVal AttrName As String) As Attribute
          >> Get
          >> Return CType(intCollec tion.Item(AttrN ame), Attribute)
          >> End Get
          >> End Property
          >> Default ReadOnly Property Item(ByVal index As Integer) As Attribute
          >> Get
          >> Return CType(intCollec tion.Item(index ), Attribute)
          >> End Get
          >> End Property
          >>
          >> Public Function GetEnumerator() As System.Collecti ons.IEnumerator
          >> Implements System.Collecti ons.IEnumerable .GetEnumerator
          >> Return intCollection.G etEnumerator
          >> End Function
          >>
          >> Public Function Contains(ByVal attrName As String) As Boolean
          >> For Each attr As Attribute In intCollection
          >> If attr.Name.ToLow er = attrName.ToLowe r Then
          >> Return True
          >> End If
          >> Next
          >> Return False
          >> End Function
          >>
          >> End Class
          >>
          >>
          >> Any code that references Attribute.Attri butes never returns a null
          >> reference, it always appears to return the correct collection object. But
          >> when you then use Attribute.Attri butes.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.
          >>
          >>
          >> "Milosz Skalecki" <mily242@REMOVE ITwp.pl> wrote in message
          >> news:EE0E0F5D-1CF3-4B76-B7F3-30518EDB7695@mi crosoft.com...[color=darkred]
          >> > Hi there,
          >> >
          >> > This is because web application is being unloaded from appdomain and
          >> > all
          >> > shared variables are lost. Try to not use static memebrs in ASP.NET
          >> > applications, use Application state + Application_Sta rt/Application_End
          >> > events instead.
          >> >
          >> > Please find detailed explanation of this problem here
          >> > http://www.devsource.com/article2/0,1895,1876120,00.asp
          >> > --
          >> > Milosz Skalecki
          >> > MCP, MCAD
          >> >
          >> >
          >> > "Simon" wrote:
          >> >
          >> >> 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 AttributeCollec tion
          >> >>
          >> >> Implements IEnumerable
          >> >>
          >> >> Protected intCollection As New Collection
          >> >>
          >> >> Public Overridable Sub Add(ByVal Attr As Attribute)
          >> >> intCollection.A dd(Attr, Attr.Name)
          >> >> End Sub
          >> >>
          >> >> Public Sub Remove(ByVal Attr As Attribute)
          >> >> intCollection.R emove(Attr.Name )
          >> >> End Sub
          >> >>
          >> >> Public Sub Remove(ByVal AttrName As String)
          >> >> intCollection.R emove(AttrName)
          >> >> End Sub
          >> >>
          >> >> Public Function Count() As Integer
          >> >> Return intCollection.C ount
          >> >> End Function
          >> >>
          >> >> Default ReadOnly Property Item(ByVal AttrName As String) As
          >> >> Attribute
          >> >> Get
          >> >> Return CType(intCollec tion.Item(AttrN ame), Attribute)
          >> >> End Get
          >> >> End Property
          >> >> Default ReadOnly Property Item(ByVal index As Integer) As
          >> >> Attribute
          >> >> Get
          >> >> Return CType(intCollec tion.Item(index ), Attribute)
          >> >> End Get
          >> >> End Property
          >> >>
          >> >> Public Function GetEnumerator() As System.Collecti ons.IEnumerator
          >> >> Implements System.Collecti ons.IEnumerable .GetEnumerator
          >> >> Return intCollection.G etEnumerator
          >> >> 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.
          >> >>
          >> >>
          >> >>
          >> >>[/color]
          >>
          >>
          >>[/color][/color]


          Comment

          • Milosz Skalecki

            #6
            Re: Shared Properties disappearing

            Howdy,

            In this case I would use thread safe singleton:

            In this article you will find how to create static fields safely in
            multithread environment.
            Also read final part of the following article:


            Hope this helps
            --
            Milosz Skalecki
            MCP, MCAD


            "Simon" wrote:
            [color=blue]
            > 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.
            >
            > "Milosz Skalecki" <mily242@REMOVE ITwp.pl> wrote in message
            > news:E856044A-2701-49D9-A31A-E06670ABA734@mi crosoft.com...[color=green]
            > > 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 (synchronizatio n, 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
            > > --
            > > Milosz Skalecki
            > > MCP, MCAD
            > >
            > >
            > > "Simon" wrote:
            > >[color=darkred]
            > >> 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 AttributeCollec tion
            > >>
            > >> Public Shared Sub RefreshCache()
            > >>
            > >> ' Code to build the collection in here....
            > >>
            > >> End Sub
            > >>
            > >> Public Shared ReadOnly Property Attributes() As AttributeCollec tion
            > >> Get
            > >> If _attributes Is Nothing Then
            > >> RefreshCache()
            > >> End If
            > >> Return _attributes
            > >> End Get
            > >>
            > >> End Property
            > >>
            > >> End Class
            > >>
            > >> Public Class AttributeCollec tion
            > >>
            > >> Implements IEnumerable
            > >>
            > >> Protected intCollection As New Collection
            > >>
            > >> Public Overridable Sub Add(ByVal Attr As Attribute)
            > >> intCollection.A dd(Attr, Attr.Name)
            > >> End Sub
            > >>
            > >> Public Sub Remove(ByVal Attr As Attribute)
            > >> intCollection.R emove(Attr.Name )
            > >> End Sub
            > >>
            > >> Public Sub Remove(ByVal AttrName As String)
            > >> intCollection.R emove(AttrName)
            > >> End Sub
            > >>
            > >> Public Function Count() As Integer
            > >> Return intCollection.C ount
            > >> End Function
            > >>
            > >> Default ReadOnly Property Item(ByVal AttrName As String) As Attribute
            > >> Get
            > >> Return CType(intCollec tion.Item(AttrN ame), Attribute)
            > >> End Get
            > >> End Property
            > >> Default ReadOnly Property Item(ByVal index As Integer) As Attribute
            > >> Get
            > >> Return CType(intCollec tion.Item(index ), Attribute)
            > >> End Get
            > >> End Property
            > >>
            > >> Public Function GetEnumerator() As System.Collecti ons.IEnumerator
            > >> Implements System.Collecti ons.IEnumerable .GetEnumerator
            > >> Return intCollection.G etEnumerator
            > >> End Function
            > >>
            > >> Public Function Contains(ByVal attrName As String) As Boolean
            > >> For Each attr As Attribute In intCollection
            > >> If attr.Name.ToLow er = attrName.ToLowe r Then
            > >> Return True
            > >> End If
            > >> Next
            > >> Return False
            > >> End Function
            > >>
            > >> End Class
            > >>
            > >>
            > >> Any code that references Attribute.Attri butes never returns a null
            > >> reference, it always appears to return the correct collection object. But
            > >> when you then use Attribute.Attri butes.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.
            > >>
            > >>
            > >> "Milosz Skalecki" <mily242@REMOVE ITwp.pl> wrote in message
            > >> news:EE0E0F5D-1CF3-4B76-B7F3-30518EDB7695@mi crosoft.com...
            > >> > Hi there,
            > >> >
            > >> > This is because web application is being unloaded from appdomain and
            > >> > all
            > >> > shared variables are lost. Try to not use static memebrs in ASP.NET
            > >> > applications, use Application state + Application_Sta rt/Application_End
            > >> > events instead.
            > >> >
            > >> > Please find detailed explanation of this problem here
            > >> > http://www.devsource.com/article2/0,1895,1876120,00.asp
            > >> > --
            > >> > Milosz Skalecki
            > >> > MCP, MCAD
            > >> >
            > >> >
            > >> > "Simon" wrote:
            > >> >
            > >> >> 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 AttributeCollec tion
            > >> >>
            > >> >> Implements IEnumerable
            > >> >>
            > >> >> Protected intCollection As New Collection
            > >> >>
            > >> >> Public Overridable Sub Add(ByVal Attr As Attribute)
            > >> >> intCollection.A dd(Attr, Attr.Name)
            > >> >> End Sub
            > >> >>
            > >> >> Public Sub Remove(ByVal Attr As Attribute)
            > >> >> intCollection.R emove(Attr.Name )
            > >> >> End Sub
            > >> >>
            > >> >> Public Sub Remove(ByVal AttrName As String)
            > >> >> intCollection.R emove(AttrName)
            > >> >> End Sub
            > >> >>
            > >> >> Public Function Count() As Integer
            > >> >> Return intCollection.C ount
            > >> >> End Function
            > >> >>
            > >> >> Default ReadOnly Property Item(ByVal AttrName As String) As
            > >> >> Attribute
            > >> >> Get
            > >> >> Return CType(intCollec tion.Item(AttrN ame), Attribute)
            > >> >> End Get
            > >> >> End Property
            > >> >> Default ReadOnly Property Item(ByVal index As Integer) As
            > >> >> Attribute
            > >> >> Get
            > >> >> Return CType(intCollec tion.Item(index ), Attribute)
            > >> >> End Get
            > >> >> End Property
            > >> >>
            > >> >> Public Function GetEnumerator() As System.Collecti ons.IEnumerator
            > >> >> Implements System.Collecti ons.IEnumerable .GetEnumerator
            > >> >> Return intCollection.G etEnumerator
            > >> >> 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.
            > >> >>
            > >> >>
            > >> >>
            > >> >>
            > >>
            > >>
            > >>[/color][/color]
            >
            >
            >[/color]

            Comment

            Working...