What is the best way of keeping a server-side object alive between page refreshes?

D

damian.jolly

Is persisting objects to the SESSION the best way to prevent an object
from being created every time a page is refreshed?

It's a bit complicated but I'll try to be brief...

I am using a guage from Fusion Charts Instrumentation Suite to show the
speed of machinary in a factory. The guage updates every 5 seconds.

The guage update does not cause a refresh of the page that it is on,
however it allows the definition of a DataStreamUrl and RefreshInterval
properties that poll a data provider .aspx page that constantly returns
the updated value in the form of &value=x

This data provider page creates a OPCDataProvider object that has
properties and methods that can retrieve data from machinery in the
factory. Each time this page is polled, it is refreshed.

To prevent the object (and it's connection to the OPC Server) from
being created every 5 seconds, I persist the object with the SESSION
command...
Imports System.text

Namespace SAGEFrontline.Portal
Public Class DataProvider
Inherits System.Web.UI.Page

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim OPCDataProvider As SAGEFrontline.Portal.OPCDataProvider
If Session("OPCDataProder") Is Nothing Then
OPCDataProvider = New
SAGEFrontline.Portal.OPCDataProvider

OPCDataProvider.AddOPCItem("Channel_0_User_Defined.Ramp.Ramp1")
Else
OPCDataProvider = CType(Session("OPCDataProder"),
SAGEFrontline.Portal.OPCDataProvider)
End If

Response.Expires = 0
Response.ContentType = "text/xml"
Response.Write("&value=" &
OPCDataProvider.ReadOPCItem("Channel_0_User_Defined.Ramp.Ramp1"))

Session("OPCDataProder") = OPCDataProvider

End Sub

End Class
End Namespace

Is this the best way to handle a server-side object? It seems to work
fine.

Cheers

Damian
 
K

Kevin Spencer

There isn't a best way. What you have to do is to look at all the available
options and tools, measure them against your requirements, and make a
decision from there. Session State is useful for persistence of objects
whose lifetime's span more than one page, or for objects that can't be
serialized to ViewState and must be persisted for multiple postbacks.
Session times out after 20 minutes without a request from that client. So,
in this case, I'd say you have chosen correctly.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
G

Guest

If everybody in your company has access to this object, a better choice would
be to persist it in the web cache. I'd wrap it in a singleton, add it to the
web cache in Global.asax.cs' Application_Start event, and add an event
handler in case the object is ever chucked out by the cache, due to age or
memory pressure, that would then re-add the object to the cache.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top