Static Functions in Multi-user web app

G

Guest

Is there any performance considerations or any other concerns about having a
Class with private constructor and all static functions? One particular
function in question creates a few new instances of COM objects (through COM
Interop), and makes lots of calls to these unmanaged COM objects.
There's a small debate in our team if having this portion of code inside
static function is appropriate for this multi-user web application that will
probably server many simultaneous requests. Should this logic be inside an
instance class?
 
M

Marina Levit [MVP]

I would imagine you need to make sure that accesses to the COM objects are
thread safe, since you only have one instance of those. If you do, that may
end up being a bottleneck when you have many users hitting the system, which
would mean you need to make all your methods instance methods using separate
instances of the COM objects.
 
N

Nicholas Paldino [.NET/C# MVP]

WebMatrix,

If the static function is not modifying state, then I don't see why not.
However, I do think that using the COM objects will be a problem. Not
because of the static nature of the functions, but you have to be sure that
you have set up the threading correctly to use COM objects in your project
(you need to set AspCompat = true in order for COM objects to work correctly
in ASP.NET, and you are going to take a little bit of a performance hit, I
believe).

Given all that, if your functions are static, and you are not modifying
any stored state, then there is no reason you can't do this. The problem
inherently doesn't come from whether or it is static or an instance method.
If you have to share the state between calls to the method (either static
state, or the state of the instance), and multiple threads are going to hit
it (like in a web environment), then you are going to have to synchronize
access to that resource.

Hope this helps.
 
G

Guest

The COM object itself has no state. All methods of this COM object are of
"request/response" nature. Each call to a static function creates COM objects
and destroy them when it's done with them. Am I understaning correctly that
each call to static function will work with its own instance of COM object
and its data? So thread safety would not be a problem?

Marina Levit said:
I would imagine you need to make sure that accesses to the COM objects are
thread safe, since you only have one instance of those. If you do, that may
end up being a bottleneck when you have many users hitting the system, which
would mean you need to make all your methods instance methods using separate
instances of the COM objects.

Thank you for your reply. There's no
 
N

Nicholas Paldino [.NET/C# MVP]

WebMatrix,

Yes, each call to the COM object will be safe, but you have to consider
the apartment model. You have to make sure that the calls are on pages/web
services where AspCompat is set to true, so that the apartment model can be
set up correctly.
 
G

Guest

If the static function is not modifying state, then I don't see why not.
However, I do think that using the COM objects will be a problem. Not
because of the static nature of the functions, but you have to be sure that
you have set up the threading correctly to use COM objects in your project
(you need to set AspCompat = true in order for COM objects to work correctly
in ASP.NET, and you are going to take a little bit of a performance hit, I
believe).

Given all that, if your functions are static, and you are not modifying
any stored state, then there is no reason you can't do this. The problem
inherently doesn't come from whether or it is static or an instance method.
If you have to share the state between calls to the method (either static
state, or the state of the instance), and multiple threads are going to hit
it (like in a web environment), then you are going to have to synchronize
access to that resource.

Hope this helps.

Thank you. It does help.

Right, no state between the calls. Like I said in a previous post; it's
"request/response" style calls.
but you have to be sure that you have set up the threading correctly to use COM objects in your project
(you need to set AspCompat = true in order for COM objects to work correctly
in ASP.NET

Could you elaborate or point to some URL.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top