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

How to share objects in an ASP.NET application

P: n/a
I am currently developing a statistics system in ASP.NET, and need to share
information about the customers websites, in this application.

(I have simplified my code, to make my project easier to explain.)

The simple version of the system is like this : A customer inserts HTML code
on his webpage, which contacts my statistics server each time the customers
website recieves a hit - like a classic "register website traffic" system.

When the customers hit reaches my web application, I need to maintain info
and state about the customers website - therefore I have a "website" class,
which hold information about the customer website (raw hits, unique hits,
website name, hit today ect).

So the first time a customer hits my servers, I need to instantiate an
instance of the website class, and update the information in this class.
Next time I recieve a request for the same customer, I only need to update
the websites information in the instance of that class.

So, here is a picture of how it works today, when the first hit comes in :

1. Customer #100 sends a request to my statistics application.
2. I have no information about customer #100, so I instantiate a new
instance of the "website" class.
3. I update information, and sets the "unique hits" variable to 1 (as this
is the first hit).

After this, all hits coming after the first hit, only updates information in
the "website" class (ie. adds 1 to the "unique hits" variabel) - like in
step 3.

I think (and hope) all the above is readable and understandable.

My problem now, is how I share these website classes in the global
application ????

Today I have a shared collection, which hold each instance of the website
class - like this :

public shared websites as new sortedlist

This shared collection can be accessed from all classes in my application,
and actually this works fine.

But, when my collection of websites reaches a certain limit (about 2500
"website" classes), the application starts to become VERY slow, and after
10-15 minutes it totally slows down and becomes useless.

And listen to me - we are talking VERY SLOW here - it runs on dual xeon HP
server, so hardware is enough for testcase ...

My question is now, if I am doing this the right way ??? How are you guys
shared objects among pages in an ASP.NET application ???

Is there are correct way to share objects globally in an ASP.NET application
?
-
Regards,
Tony Fonager, Denmark
Nov 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Check out the Appplication Cache.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Tony Fonager" <tony@dontspamthisemailadress_fonager.dk> wrote in message
news:#r**************@tk2msftngp13.phx.gbl...
I am currently developing a statistics system in ASP.NET, and need to share information about the customers websites, in this application.

(I have simplified my code, to make my project easier to explain.)

The simple version of the system is like this : A customer inserts HTML code on his webpage, which contacts my statistics server each time the customers website recieves a hit - like a classic "register website traffic" system.

When the customers hit reaches my web application, I need to maintain info
and state about the customers website - therefore I have a "website" class, which hold information about the customer website (raw hits, unique hits,
website name, hit today ect).

So the first time a customer hits my servers, I need to instantiate an
instance of the website class, and update the information in this class.
Next time I recieve a request for the same customer, I only need to update
the websites information in the instance of that class.

So, here is a picture of how it works today, when the first hit comes in :
1. Customer #100 sends a request to my statistics application.
2. I have no information about customer #100, so I instantiate a new
instance of the "website" class.
3. I update information, and sets the "unique hits" variable to 1 (as this
is the first hit).

After this, all hits coming after the first hit, only updates information in the "website" class (ie. adds 1 to the "unique hits" variabel) - like in
step 3.

I think (and hope) all the above is readable and understandable.

My problem now, is how I share these website classes in the global
application ????

Today I have a shared collection, which hold each instance of the website
class - like this :

public shared websites as new sortedlist

This shared collection can be accessed from all classes in my application,
and actually this works fine.

But, when my collection of websites reaches a certain limit (about 2500
"website" classes), the application starts to become VERY slow, and after
10-15 minutes it totally slows down and becomes useless.

And listen to me - we are talking VERY SLOW here - it runs on dual xeon HP
server, so hardware is enough for testcase ...

My question is now, if I am doing this the right way ??? How are you guys
shared objects among pages in an ASP.NET application ???

Is there are correct way to share objects globally in an ASP.NET application ?
-
Regards,
Tony Fonager, Denmark

Nov 18 '05 #2

P: n/a
Hello Tony!
If you make your class a Singleton and declare yout collection i
global.asax?
The Singleton design pattern lets you use tha class without instanciating it
first, it keeps a private member of it's own type and uses that instance if
it is instanciated.

to use a Singleton class in code, just use it like this:

mynamespace.myclass.Instance.myMethod()

Instance is a public function.

Example code:

Class myClass

Public Shared moMyClass As myClass = Nothing

Private Sub new()
'private constructor disallowing other to create object directly
End Sub

Friend Shared Function Instance() As myClass
If moMyClass = Nothing Then
'First instance
'return a new object
moMyClass = New myClass()
Return moMyClass
Else
Return moMyClass
End If
End Function

Public Function myMethod()
'do something clever
End Function
End Class

Hope it helps!
"Tony Fonager" <tony@dontspamthisemailadress_fonager.dk> skrev i meddelandet
news:#r**************@tk2msftngp13.phx.gbl...
I am currently developing a statistics system in ASP.NET, and need to share information about the customers websites, in this application.

(I have simplified my code, to make my project easier to explain.)

The simple version of the system is like this : A customer inserts HTML code on his webpage, which contacts my statistics server each time the customers website recieves a hit - like a classic "register website traffic" system.

When the customers hit reaches my web application, I need to maintain info
and state about the customers website - therefore I have a "website" class, which hold information about the customer website (raw hits, unique hits,
website name, hit today ect).

So the first time a customer hits my servers, I need to instantiate an
instance of the website class, and update the information in this class.
Next time I recieve a request for the same customer, I only need to update
the websites information in the instance of that class.

So, here is a picture of how it works today, when the first hit comes in :
1. Customer #100 sends a request to my statistics application.
2. I have no information about customer #100, so I instantiate a new
instance of the "website" class.
3. I update information, and sets the "unique hits" variable to 1 (as this
is the first hit).

After this, all hits coming after the first hit, only updates information in the "website" class (ie. adds 1 to the "unique hits" variabel) - like in
step 3.

I think (and hope) all the above is readable and understandable.

My problem now, is how I share these website classes in the global
application ????

Today I have a shared collection, which hold each instance of the website
class - like this :

public shared websites as new sortedlist

This shared collection can be accessed from all classes in my application,
and actually this works fine.

But, when my collection of websites reaches a certain limit (about 2500
"website" classes), the application starts to become VERY slow, and after
10-15 minutes it totally slows down and becomes useless.

And listen to me - we are talking VERY SLOW here - it runs on dual xeon HP
server, so hardware is enough for testcase ...

My question is now, if I am doing this the right way ??? How are you guys
shared objects among pages in an ASP.NET application ???

Is there are correct way to share objects globally in an ASP.NET application ?
-
Regards,
Tony Fonager, Denmark

Nov 18 '05 #3

P: n/a
Do you want me to add the "website" class into the application cache ?

I cannot see how this can be done - I need to call functions on this
"website" object and set properties again and again - how does this work, if
the object is put into the cache ?

-
Regards,
Tony

"Kevin Spencer" <ks******@takempis.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Check out the Appplication Cache.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

Nov 18 '05 #4

P: n/a
HenkeBenke,

I think I understand the concept of singletons, but anyhow I cannot see how
this makes it a better solution than the way I am doing it today !

Does singletons use less memory, are they faster or what is it (sorry, no
experience with singleton design patter).

And is there a difference when sharing the collection in globas.asax instead
of my page ?
-
Regards,
Tony Fonager

"HenkeBenke" <he************@hotmail.com> wrote in message
news:ec*************@TK2MSFTNGP11.phx.gbl...
Hello Tony!
If you make your class a Singleton and declare yout collection i
global.asax?
The Singleton design pattern lets you use tha class without instanciating it first, it keeps a private member of it's own type and uses that instance if it is instanciated.

to use a Singleton class in code, just use it like this:

mynamespace.myclass.Instance.myMethod()

Instance is a public function.

Example code:

Class myClass

Public Shared moMyClass As myClass = Nothing

Private Sub new()
'private constructor disallowing other to create object directly
End Sub

Friend Shared Function Instance() As myClass
If moMyClass = Nothing Then
'First instance
'return a new object
moMyClass = New myClass()
Return moMyClass
Else
Return moMyClass
End If
End Function

Public Function myMethod()
'do something clever
End Function
End Class

Nov 18 '05 #5

P: n/a
You post made me think ...

Why would I like only one instance of my website class ? My design needs me
to have an instance for each website (customer website), so that I can
differentiate each customers web traffic, recorded by my system!

Dont you agree ?
Regards,
Tony
Nov 18 '05 #6

P: n/a
Hi Tony,
Do you want me to add the "website" class into the application cache ?

I cannot see how this can be done - I need to call functions on this
"website" object and set properties again and again - how does this work, if the object is put into the cache ?
Application cache is simply a memory space. When you cache an object, it is
stored in memory. You can certainly use it any time. You could store a
Collection of "website" objects in the Application cache. Example:

((ArrayList) Cache["websCollection"]).Add(new website()); // Store

website w = ((ArrayList) Cache["websCollection"])[0]; // Fetch

I would recommend using the Cache, as it is thread-safe.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Tony Fonager" <tony@dontspamthisemailadress_fonager.dk> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl... Do you want me to add the "website" class into the application cache ?

I cannot see how this can be done - I need to call functions on this
"website" object and set properties again and again - how does this work, if the object is put into the cache ?

-
Regards,
Tony

"Kevin Spencer" <ks******@takempis.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Check out the Appplication Cache.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living


Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.