calling a static method in a windows service?

  • Thread starter Thread starter KK
  • Start date Start date
K

KK

Hi

Is there a way to call a static method in a service?

For example, NT service is an instance of a class.

ServicesToRun = new System.ServiceProcess.ServiceBase[] { new FooClass() };

I want to findout wether a static mthod in this FooClass can be
accessed from another assembly in another application ?

regards
KK
 
Hi Vipul,

not really,

I managed to get it to work.
in the service class like any other static method you just define a method,
say

public static void something(){
}

from the other application which you want to call (say a winfoms
applcation),
when you compile the winforms app, just give a reference to the service
class
such as

csc /target:winexe /debug /out:bin\debug\TestApp.exe /lib:bin\debug
/r:SystemWatch.exe *.cs

SystemWatch.exe in this case is my service.

now from the winforms app, I can call the static method just as usual
but still am stuck at another point.. hope to get it fixed..
lets see..
KK
 
The thing is, even if you can get it to run, this method will run in the
context of your appdomain - not in that of the windows service. Which means
you are not going to get the results that the windows service would have
given you.
 
You are right.

I think thats why Vipul also said that its not possible.

well... damn....

See, I just want to have a.... lets say a hashtable filled with some data
in memory. This has to be in a seperate process. Why i need those in
memory always is because those values I query quite often, I can't
spend time to query database and get those values for every request.

From another process I want to access that hashtable to get values.

I thought having a service runing (filled with hashtable data)
will do the trick. but it doesnt :(

Any ideas?

(Pardon me for the werid thing am going to do.. but i need it)

regards
KK
 
KK said:
See, I just want to have a.... lets say a hashtable filled with some data
in memory. This has to be in a seperate process. Why i need those in
memory always is because those values I query quite often, I can't
spend time to query database and get those values for every request.

From another process I want to access that hashtable to get values.

I thought having a service runing (filled with hashtable data)
will do the trick. but it doesnt :(


I guess its more to do with your implementation of the service. If effect
the service object is a singleton - that is there is one instance on your
machine. Your service process will need to listen for requests from clients,
maybe that is the part that is failing in your case.

Do you need a windows service? The main reason for using a service is to be
able to run the code under an account other than the interactive user's
account (either a higher priviledge or a lower priviledge). If you don't
need to do that then what about using a web service? Again, using this you
can implement a singleton to provide the values.

Richard
 
Hi Richard

Refering to my first post, this was to test some code posted by
MSDN. That shows how to secure ASP.NET files by hashing.

In that implemntation it stores the hashes on the database and
then query it everytime when a request come to the page. which
I think is a vey heavy (for the system)

That is why I wanted to have a chched set of data in memory
so I dont have to read the hash values from db every time to compare

Anyhow I manage to get it worked. I now use a Shared Property Manager
in COM+ Now the only overhead is what you get from COM+ to
..NET and vice versa.

But for my testing purposes this works.

Btw, thanks to all of you who shared your thoughts

regards
KK
 
Back
Top