By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,710 Members | 1,613 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,710 IT Pros & Developers. It's quick & easy.

Can I store a C# Class Instance to the Server Cache?

P: n/a
Of course, I can store a C# class instance to the Server Cache
(this.Context.Cache). I've tried it. My question is, will this destroy the
scalability of my application?

My background is VB6. With VB6 we learned very early on that you do *NOT*
store VB6 class instances on the server. That is because a VB6 class can
only be accessed on the thread on which it is created (thread affinity).
This is just incompatible with stateless web architecture since with this
architecture every client request will likely be served by different ASP
pages executing on different threads. However, if VB6 classes are stored to
the Session object, then the ASP execution is going to block until the
thread on which the class instance was created becomes available.

Is this limitation still inherent with .NET classes, or are .NET class
instances 'safe' in this regard?

(Of course, the solution would be to serialize to XML and store the XML
string, but if I can safely store the class reference, why bother?)

Thanks for your advice!

- Joseph Geretz -
Feb 14 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Joseph Geretz" <jg*****@nospam.comwrote in message
news:eO****************@TK2MSFTNGP02.phx.gbl...
Of course, I can store a C# class instance to the Server Cache
(this.Context.Cache). I've tried it. My question is, will this destroy the
scalability of my application?

My background is VB6. With VB6 we learned very early on that you do *NOT*
store VB6 class instances on the server. That is because a VB6 class can
only be accessed on the thread on which it is created (thread affinity).
This is not a problem with .NET. You have to make sure the class is
serializable.

John
Feb 14 '07 #2

P: n/a
Hi John,

Thanks for your response. I'm not sure what you mean by serializable. Do you
mean that I need to serialize this explicitly into the Cache, or that the
Framework will take care of the serialization for me if the class is
serializable?

Here is that class which I am caching.

internal class SessionCache
{
public string SID;
public string UID;
public string PWD;
public DataTable Profile;
}

I know this is 'working' since I'm able to retrieve this from the cache on
successive Web Method requests. Is this actually being serialized into the
cache for me 'under the hood'? Does it need to be? Do I need to do anything
explicitly to serialize this into the cache?

Thanks for your advice!

- Joseph Geretz -

"John Saunders" <john.saunders at trizetto.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
"Joseph Geretz" <jg*****@nospam.comwrote in message
news:eO****************@TK2MSFTNGP02.phx.gbl...
>Of course, I can store a C# class instance to the Server Cache
(this.Context.Cache). I've tried it. My question is, will this destroy
the scalability of my application?

My background is VB6. With VB6 we learned very early on that you do *NOT*
store VB6 class instances on the server. That is because a VB6 class can
only be accessed on the thread on which it is created (thread affinity).

This is not a problem with .NET. You have to make sure the class is
serializable.

John


Feb 14 '07 #3

P: n/a
"Joseph Geretz" <jg*****@nospam.comwrote in message
news:%2***************@TK2MSFTNGP03.phx.gbl...
Hi John,

Thanks for your response. I'm not sure what you mean by serializable. Do
you mean that I need to serialize this explicitly into the Cache, or that
the Framework will take care of the serialization for me if the class is
serializable?

Here is that class which I am caching.

internal class SessionCache
{
public string SID;
public string UID;
public string PWD;
public DataTable Profile;
}

I know this is 'working' since I'm able to retrieve this from the cache on
successive Web Method requests. Is this actually being serialized into the
cache for me 'under the hood'? Does it need to be? Do I need to do
anything explicitly to serialize this into the cache?
Joseph, I'm sorry, I wasn't thinking clearly. I was thinking about
ViewState, which _does_ require instances to be serializable. Clearly, the
ASP.NET Cache object doesn't need anything more than your class provides.

John
Feb 14 '07 #4

P: n/a
Thanks John,
Clearly, the ASP.NET Cache object doesn't need anything more than your
class provides
I know this works, but will I run into any threading problems? In other
words, let's say the user logs in with an instance of the ASMX page
executing on thread 345. The SessionCache class instance will be created on
thread 345 and a reference to that class instance will be stuffed into the
Server Cache. Let's say the next transaction for this user's session is
served up by an ASMX page executing on thread 987. Will access to the
SessionCache class instance involve a thread context switch from 987 to 345?
This is the sort of trap which looks like it's working initially, but as
usage scales up, this sort of problem will start to manifest itself in a big
way. (Forgive me for stressing the point, but a lot VB6 developers were
burned badly by saving class instances on the Server and I want to make sure
that .NET class instances aren't succeptible to the same type of thread
affinity problems.)

Thanks!

- Joseph Geretz -

"John Saunders" <john.saunders at trizetto.comwrote in message
news:OO**************@TK2MSFTNGP03.phx.gbl...
"Joseph Geretz" <jg*****@nospam.comwrote in message
news:%2***************@TK2MSFTNGP03.phx.gbl...
>Hi John,

Thanks for your response. I'm not sure what you mean by serializable. Do
you mean that I need to serialize this explicitly into the Cache, or that
the Framework will take care of the serialization for me if the class is
serializable?

Here is that class which I am caching.

internal class SessionCache
{
public string SID;
public string UID;
public string PWD;
public DataTable Profile;
}

I know this is 'working' since I'm able to retrieve this from the cache
on successive Web Method requests. Is this actually being serialized into
the cache for me 'under the hood'? Does it need to be? Do I need to do
anything explicitly to serialize this into the cache?

Joseph, I'm sorry, I wasn't thinking clearly. I was thinking about
ViewState, which _does_ require instances to be serializable. Clearly, the
ASP.NET Cache object doesn't need anything more than your class provides.

John


Feb 15 '07 #5

P: n/a
"Joseph Geretz" <jg*****@nospam.comwrote in message
news:eJ**************@TK2MSFTNGP03.phx.gbl...
Thanks John,
>Clearly, the ASP.NET Cache object doesn't need anything more than your
class provides

I know this works, but will I run into any threading problems?
Joseph, all of that COM-era thread affinity stuff is gone in .NET.

John
Feb 15 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.