caching in WinForm and ASP.NET

  • Thread starter Thread starter Sunish Abraham
  • Start date Start date
S

Sunish Abraham

I need to adding caching capability to our application and we "get" data
from our business layer code ; so initially I wanted to use something in
..Net framework that provided caching and found that the "caching" mechanism
was implemented in System.Web ; initially I didn't want to add System.Web
reference to our business object because it was used from our web
applications as well as WinForm, console applications ; so I thought I would
handle the "checking" to see if the business object was running in the
HttpContext and then use System.Web.HttpRunTime.Cache (if in ASP.NET) or
EntLib Caching (other) ; I wanted to test something, so I created a console
application and a classlibrary ; in the classlibrary, I referenced
system.web and added the following shared method:
Public Shared Function GetData() As String
Dim strCachedInfo As String =
CType(System.Web.HttpRuntime.Cache("CACHEDINFO"), String)

If strCachedInfo Is Nothing Then
strCachedInfo = "Hello world..."
System.Web.HttpRuntime.Cache.Insert("CACHEDINFO", strCachedInfo,
Nothing, DateTime.Now.AddSeconds(15), TimeSpan.Zero)
End If

Return (strCachedInfo)
End Function

and in the console application, i referenced the classlibrary and called the
GetData method...
Console.WriteLine(ClassLibrary1.Class1.GetData())

to my confusion, it worked?? ; I don't understand why if the classlibrary
was running in the "context" of the console process did i not get an
exception when calling System.Web.HttpRuntime.Cache? I read that http
context was implemented using
System.Runtime.Remoting.Messaging.CallContext...does this have something
with why this worked? am I not understanding the caching mechanism
correctly? thanks for light you can shed on this!!

Sunish Abraham
 
The cached context simply runs as an instance in the IIS webserver process
space. If you can call into that process space using current context, you
will be able to retrieve the values. no surprises there.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
 
Thanks for the reply Alvin...but I am bit confused. I tested this "console"
application on my laptop which is WinXP with IIS installed (so I can
understand your reply)...so then I copied the executable (console app) and
the dll (class library) to a WinXP machine without IIS installed and it
worked the same as on my laptop. If there is no IIS on a machine, why isn't
there an exception thrown when calling System.Web.HttpRuntime.Cache? Thanks
again for your assistance!

Sunish

Alvin Bruney - ASP.NET MVP said:
The cached context simply runs as an instance in the IIS webserver process
space. If you can call into that process space using current context, you
will be able to retrieve the values. no surprises there.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------



Sunish Abraham said:
I need to adding caching capability to our application and we "get" data
from our business layer code ; so initially I wanted to use something in
.Net framework that provided caching and found that the "caching" mechanism
was implemented in System.Web ; initially I didn't want to add System.Web
reference to our business object because it was used from our web
applications as well as WinForm, console applications ; so I thought I would
handle the "checking" to see if the business object was running in the
HttpContext and then use System.Web.HttpRunTime.Cache (if in ASP.NET) or
EntLib Caching (other) ; I wanted to test something, so I created a console
application and a classlibrary ; in the classlibrary, I referenced
system.web and added the following shared method:
Public Shared Function GetData() As String
Dim strCachedInfo As String =
CType(System.Web.HttpRuntime.Cache("CACHEDINFO"), String)

If strCachedInfo Is Nothing Then
strCachedInfo = "Hello world..."
System.Web.HttpRuntime.Cache.Insert("CACHEDINFO", strCachedInfo,
Nothing, DateTime.Now.AddSeconds(15), TimeSpan.Zero)
End If

Return (strCachedInfo)
End Function

and in the console application, i referenced the classlibrary and called the
GetData method...
Console.WriteLine(ClassLibrary1.Class1.GetData())

to my confusion, it worked?? ; I don't understand why if the classlibrary
was running in the "context" of the console process did i not get an
exception when calling System.Web.HttpRuntime.Cache? I read that http
context was implemented using
System.Runtime.Remoting.Messaging.CallContext...does this have something
with why this worked? am I not understanding the caching mechanism
correctly? thanks for light you can shed on this!!

Sunish Abraham
 
Back
Top