473,796 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Singleton in web service?

Bob
Does anyone know of any adverse affects of using singleton data access
objects in an ASP.NET Web Service? I've been forced to implement it this way
but it just doesn't seem right to me. I haven't been able to find a single
example of someone doing it this way and that's usually not a good sign. I'm
afraid that there may be subtle problems that won't show up quickly because
the production environment will not be hitting the web service at a high
rate. Of course I'm also concerned about not subtle problems as well but
they're easier to find.

--
Thanks,
Bob

Nov 19 '05 #1
5 3998
As long as the singleton doesn't maintain state, it's a reasonable
design. The Microsoft Data Access block class uses all static methods
- which is not a textbook singleton design, but does function the
similarly.

The real problem is state. If the singleton uses fields to store data
between method calls, then you need to be multi-threaded safe, which
can be a performance hit. The DAB does maintain some state (it caches
stored procedure parameter information like names and types) and it
uses locking to avoid corruptions. I'd think MS tested this and
determined it was worth the additional complexity, and overall in this
case the code probably performs better with the caching.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 21 Dec 2004 11:05:04 -0800, Bob
<Bo*@discussion s.microsoft.com > wrote:
Does anyone know of any adverse affects of using singleton data access
objects in an ASP.NET Web Service? I've been forced to implement it this way
but it just doesn't seem right to me. I haven't been able to find a single
example of someone doing it this way and that's usually not a good sign. I'm
afraid that there may be subtle problems that won't show up quickly because
the production environment will not be hitting the web service at a high
rate. Of course I'm also concerned about not subtle problems as well but
they're easier to find.


Nov 19 '05 #2
Bob
Thanks Scott. I'm not maintaining state and the consruction is thread safe
so I'm alright there. Do you understand where a singleton is maintained
between calls to a web service? We're not storing the instance in
application state or anywhere else explicitly but it does seem to persist.
This is what worries me the most about this design, not knowing how it's
persisted. I always thought that web services should be designed without any
state between calls, except for remoting singletons which this is not. What
gets me is that the data access classes are tiny and the overhead to create
and destroy on each call it would be trivial compared to the actual data
access.

"Scott Allen" wrote:
As long as the singleton doesn't maintain state, it's a reasonable
design. The Microsoft Data Access block class uses all static methods
- which is not a textbook singleton design, but does function the
similarly.

The real problem is state. If the singleton uses fields to store data
between method calls, then you need to be multi-threaded safe, which
can be a performance hit. The DAB does maintain some state (it caches
stored procedure parameter information like names and types) and it
uses locking to avoid corruptions. I'd think MS tested this and
determined it was worth the additional complexity, and overall in this
case the code probably performs better with the caching.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 21 Dec 2004 11:05:04 -0800, Bob
<Bo*@discussion s.microsoft.com > wrote:
Does anyone know of any adverse affects of using singleton data access
objects in an ASP.NET Web Service? I've been forced to implement it this way
but it just doesn't seem right to me. I haven't been able to find a single
example of someone doing it this way and that's usually not a good sign. I'm
afraid that there may be subtle problems that won't show up quickly because
the production environment will not be hitting the web service at a high
rate. Of course I'm also concerned about not subtle problems as well but
they're easier to find.


Nov 19 '05 #3
Are there any static fields that reference the singleton? If there is
any outstanding reference to the object the garabage collector won't
reclaim the object. How do you get a reference to the singleton to
make a data access call?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 21 Dec 2004 16:07:01 -0800, Bob
<Bo*@discussion s.microsoft.com > wrote:
Thanks Scott. I'm not maintaining state and the consruction is thread safe
so I'm alright there. Do you understand where a singleton is maintained
between calls to a web service? We're not storing the instance in
application state or anywhere else explicitly but it does seem to persist.
This is what worries me the most about this design, not knowing how it's
persisted. I always thought that web services should be designed without any
state between calls, except for remoting singletons which this is not. What
gets me is that the data access classes are tiny and the overhead to create
and destroy on each call it would be trivial compared to the actual data
access.

"Scott Allen" wrote:
As long as the singleton doesn't maintain state, it's a reasonable
design. The Microsoft Data Access block class uses all static methods
- which is not a textbook singleton design, but does function the
similarly.

The real problem is state. If the singleton uses fields to store data
between method calls, then you need to be multi-threaded safe, which
can be a performance hit. The DAB does maintain some state (it caches
stored procedure parameter information like names and types) and it
uses locking to avoid corruptions. I'd think MS tested this and
determined it was worth the additional complexity, and overall in this
case the code probably performs better with the caching.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 21 Dec 2004 11:05:04 -0800, Bob
<Bo*@discussion s.microsoft.com > wrote:
>Does anyone know of any adverse affects of using singleton data access
>objects in an ASP.NET Web Service? I've been forced to implement it this way
>but it just doesn't seem right to me. I haven't been able to find a single
>example of someone doing it this way and that's usually not a good sign. I'm
>afraid that there may be subtle problems that won't show up quickly because
>the production environment will not be hitting the web service at a high
>rate. Of course I'm also concerned about not subtle problems as well but
>they're easier to find.



Nov 19 '05 #4
Bob
Actually I have been keeping the singleton in a non-static field in the class
that uses the singleton. The constructor of the using class gets the
singleton instance via an "Instance" method of the singleton and assigns it
to the field. Member methods then use the member field for data access. To
make matters a little more complicated there are 2 levels of singletons. The
web service exposes 2 classes, each class uses a singleton instance of a
manager class which in turn uses a singleton instance of a data access class
(all the singletons are to different classes). Do you think it is better to
just get the instance inside each method that is going to use a singleton?
That is easy to do, I was just trying to save a few lines of code because one
of the classes has quite a few methods exposed and each needs access to its
singleton. I understand how these things work in regular apps (although I
generally avoid them) but I don't really understand the life cycle and
application domain of web services. Thanks for your help, I really
appreciate it.

"Scott Allen" wrote:
Are there any static fields that reference the singleton? If there is
any outstanding reference to the object the garabage collector won't
reclaim the object. How do you get a reference to the singleton to
make a data access call?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 21 Dec 2004 16:07:01 -0800, Bob
<Bo*@discussion s.microsoft.com > wrote:
Thanks Scott. I'm not maintaining state and the consruction is thread safe
so I'm alright there. Do you understand where a singleton is maintained
between calls to a web service? We're not storing the instance in
application state or anywhere else explicitly but it does seem to persist.
This is what worries me the most about this design, not knowing how it's
persisted. I always thought that web services should be designed without any
state between calls, except for remoting singletons which this is not. What
gets me is that the data access classes are tiny and the overhead to create
and destroy on each call it would be trivial compared to the actual data
access.

"Scott Allen" wrote:
As long as the singleton doesn't maintain state, it's a reasonable
design. The Microsoft Data Access block class uses all static methods
- which is not a textbook singleton design, but does function the
similarly.

The real problem is state. If the singleton uses fields to store data
between method calls, then you need to be multi-threaded safe, which
can be a performance hit. The DAB does maintain some state (it caches
stored procedure parameter information like names and types) and it
uses locking to avoid corruptions. I'd think MS tested this and
determined it was worth the additional complexity, and overall in this
case the code probably performs better with the caching.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 21 Dec 2004 11:05:04 -0800, Bob
<Bo*@discussion s.microsoft.com > wrote:

>Does anyone know of any adverse affects of using singleton data access
>objects in an ASP.NET Web Service? I've been forced to implement it this way
>but it just doesn't seem right to me. I haven't been able to find a single
>example of someone doing it this way and that's usually not a good sign. I'm
>afraid that there may be subtle problems that won't show up quickly because
>the production environment will not be hitting the web service at a high
>rate. Of course I'm also concerned about not subtle problems as well but
>they're easier to find.


Nov 19 '05 #5
The app domain in a web service is a little more 'fragile' in a way,
or perhaps more 'robust' - depending on your point of view. :)

I say this because there are many forces that can control the lifetime
of the app domain. For instance, IIS can be configured to recycle the
app domain if certain thresholds are met, the runtime will start a new
app domain if the config file changes, and so on.

I discussed this a bit in an article, but I think you'll know this.
http://odetocode.com/Articles/305.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Wed, 22 Dec 2004 18:11:02 -0800, Bob
<Bo*@discussion s.microsoft.com > wrote:
Actually I have been keeping the singleton in a non-static field in the class
that uses the singleton. The constructor of the using class gets the
singleton instance via an "Instance" method of the singleton and assigns it
to the field. Member methods then use the member field for data access. To
make matters a little more complicated there are 2 levels of singletons. The
web service exposes 2 classes, each class uses a singleton instance of a
manager class which in turn uses a singleton instance of a data access class
(all the singletons are to different classes). Do you think it is better to
just get the instance inside each method that is going to use a singleton?
That is easy to do, I was just trying to save a few lines of code because one
of the classes has quite a few methods exposed and each needs access to its
singleton. I understand how these things work in regular apps (although I
generally avoid them) but I don't really understand the life cycle and
application domain of web services. Thanks for your help, I really
appreciate it.

"Scott Allen" wrote:
Are there any static fields that reference the singleton? If there is
any outstanding reference to the object the garabage collector won't
reclaim the object. How do you get a reference to the singleton to
make a data access call?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 21 Dec 2004 16:07:01 -0800, Bob
<Bo*@discussion s.microsoft.com > wrote:
>Thanks Scott. I'm not maintaining state and the consruction is thread safe
>so I'm alright there. Do you understand where a singleton is maintained
>between calls to a web service? We're not storing the instance in
>application state or anywhere else explicitly but it does seem to persist.
>This is what worries me the most about this design, not knowing how it's
>persisted. I always thought that web services should be designed without any
>state between calls, except for remoting singletons which this is not. What
>gets me is that the data access classes are tiny and the overhead to create
>and destroy on each call it would be trivial compared to the actual data
>access.
>
>"Scott Allen" wrote:
>
>> As long as the singleton doesn't maintain state, it's a reasonable
>> design. The Microsoft Data Access block class uses all static methods
>> - which is not a textbook singleton design, but does function the
>> similarly.
>>
>> The real problem is state. If the singleton uses fields to store data
>> between method calls, then you need to be multi-threaded safe, which
>> can be a performance hit. The DAB does maintain some state (it caches
>> stored procedure parameter information like names and types) and it
>> uses locking to avoid corruptions. I'd think MS tested this and
>> determined it was worth the additional complexity, and overall in this
>> case the code probably performs better with the caching.
>>
>> --
>> Scott
>> http://www.OdeToCode.com/blogs/scott/
>>
>> On Tue, 21 Dec 2004 11:05:04 -0800, Bob
>> <Bo*@discussion s.microsoft.com > wrote:
>>
>> >Does anyone know of any adverse affects of using singleton data access
>> >objects in an ASP.NET Web Service? I've been forced to implement it this way
>> >but it just doesn't seem right to me. I haven't been able to find a single
>> >example of someone doing it this way and that's usually not a good sign. I'm
>> >afraid that there may be subtle problems that won't show up quickly because
>> >the production environment will not be hitting the web service at a high
>> >rate. Of course I'm also concerned about not subtle problems as well but
>> >they're easier to find.
>>
>>



Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2257
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application which makes calls to the service. We are using tcp channel which is using binaryformatter by default. The problem is that after a certain number of remoting calls the calls dont get through to the server. The client application makes the call and...
21
2469
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought maybe a Template can be use for that, but C# does not support Templates (will be C# generics in mid 2005). Does anyone have a solution on how the singleton pattern can be written, in C#, as a framework/ infrastructure class, so users can use this...
12
1895
by: keepyourstupidspam | last post by:
Hi, I am writing a windows service. The code runs fine when I start the service when my machine is running but it fails to start automatically when the machine reboots. The code bombs out when it reaches code that tries to access a singleton class. This is the code.
2
3901
by: mma | last post by:
hello everyone, i have a solution that contains the following project types: 1. Class library contains a singleton class that handles the data access (sql server 2005). 2. Windows service. uses the Class library's singleton class to manipulate data in the data base server. 3. Windows application.
7
2240
by: fredd00 | last post by:
Hi I'm just starting with singleton and would like to implement in my new web app. I have a question lets say i create a singleton DataHelper that holds a static SqlConnection object to share and on my page I do
9
2986
by: oleggus | last post by:
I hope, I misunderstood some basics here and it is easy to solve.. I need a singleton object running on server which can be used(write and read) by different client interfaces - for example there is webservice that simply transfer calls to this object and there is a COM Interop, that does the same. Also I need to control lifecycle and behaviour of this singleton - and for that I am going to have window application, that will start and...
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10228
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.