Saturday, March 24, 2012

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

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
DamianThere 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.
<damian.jolly@.sageautomation.com> wrote in message
news:1146653185.887362.208110@.j33g2000cwa.googlegroups.com...
> 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
>

0 comments:

Post a Comment