Windows service concept?

G

Guest

Hi, friends,

Here is a question about Windows service concept in C#.net 2005.

Suppose my service is doing a transaction, say modifying records/data of a
database. At this point, a user issues a command to stop this service? How
this service will behave? It will stop right away without rollback, or it
will finish this transaction then stop?

Any ideas, reference papers? Thanks.
 
H

Henning Krause [MVP - Exchange]

Hello,

when a service is gracefully stopped (not killed), the OnShutdown method is
called inside your service. You can then hold the stop-process until the
transaction is complete or do a rollback.

The services MMC will wait up to 30 seconds before displaying a warning
message to the user who stopped the service. But even in this case, the
process will not be terminated.

Best regards,
Henning
 
G

Guest

Thanks, Henning. I have another question:

Is that possible for a Windows service to have public properties/methods so
that other assembly may access it?

If yes, how? Any reference paper or samples? Thanks again.
 
W

William Stacey [C# MVP]

In this case, you want remote api on your service. Something like WCF would
be good.

--
William Stacey [C# MVP]


| Thanks, Henning. I have another question:
|
| Is that possible for a Windows service to have public properties/methods
so
| that other assembly may access it?
|
| If yes, how? Any reference paper or samples? Thanks again.
|
| "Henning Krause [MVP - Exchange]" wrote:
|
| > Hello,
| >
| > when a service is gracefully stopped (not killed), the OnShutdown method
is
| > called inside your service. You can then hold the stop-process until the
| > transaction is complete or do a rollback.
| >
| > The services MMC will wait up to 30 seconds before displaying a warning
| > message to the user who stopped the service. But even in this case, the
| > process will not be terminated.
| >
| > Best regards,
| > Henning
| >
| > | > > Hi, friends,
| > >
| > > Here is a question about Windows service concept in C#.net 2005.
| > >
| > > Suppose my service is doing a transaction, say modifying records/data
of a
| > > database. At this point, a user issues a command to stop this service?
How
| > > this service will behave? It will stop right away without rollback, or
it
| > > will finish this transaction then stop?
| > >
| > > Any ideas, reference papers? Thanks.
| >
| >
 
H

Henning Krause [MVP - Exchange]

Hello,

WCF will be an excellent choice unless your service must run on Windows
2000. WCF is not supported there. In that case you'll have to use another
technique. Possible choices include WSE 3.0 (to host in-process
webservices), Message Queues and .NET Remoting .

Best regards,
Henning Krause
 
M

Moty Michaely

Hello,

WCF will be an excellent choice unless your service must run on Windows
2000. WCF is not supported there. In that case you'll have to use another
technique. Possible choices include WSE 3.0 (to host in-process
webservices), Message Queues and .NET Remoting .

Best regards,
Henning Krause

William Stacey said:
In this case, you want remote api on your service. Something like WCF
would
be good.
| Thanks, Henning. I have another question:
|
| Is that possible for a Windows service to have public properties/methods
so
| that other assembly may access it?
|
| If yes, how? Any reference paper or samples? Thanks again.
|
| "Henning Krause [MVP - Exchange]" wrote:
|
| > Hello,
| >
| > when a service is gracefully stopped (not killed), the OnShutdown
method
is
| > called inside your service. You can then hold the stop-process until
the
| > transaction is complete or do a rollback.
| >
| > The services MMC will wait up to 30 seconds before displaying a
warning
| > message to the user who stopped the service. But even in this case,
the
| > process will not be terminated.
| >
| > Best regards,
| > Henning
| >
| >| > > Hi, friends,
| > >
| > > Here is a question about Windows service concept in C#.net 2005.
| > >
| > > Suppose my service is doing a transaction, say modifying
records/data
of a
| > > database. At this point, a user issues a command to stop this
service?
How
| > > this service will behave? It will stop right away without rollback,
or
it
| > > will finish this transaction then stop?
| > >
| > > Any ideas, reference papers? Thanks.
| >
| >

Hi,

Besides WCF mentioned (That is supported in Windows XP SP2 and up),
you can choose any IPC (Inter Process Communication) techniques such
as mentioned by Henning.

Take a look at this article just to get the feeling of IPC.

Moty
http://www.codeproject.com/threads/ipcworkshop.asp?df=100&forumid=14121&exp=0&select=407325
 
G

Guest

Thank you all, guys!

One more question about implementation. I listed all services running on a
selected machine by looping each element in the array returned below.

ServiceController[] controllers =
ServiceController.GetServices(selectedMachineName);

When a user selects a service from the list and wants to stop/start it, I
use the above code, loop the array again, and find the controller this user
selects by comparing controller.ServiceName with the service name of
selected service. Through this controller, the service is stopped/started.

My question is: Is there a way that I don't have to "loop the array again",
rather, I may save "something" when looping the first time? (I guess not, but
not sure).

Also, controller does not contain Startup Type, Log On As info. Any other
..net module may provide those info?

Thanks a lot !
 
H

Henning Krause [MVP - Exchange]

Hello Andrew,

if you have the name of the service, you can construct a service controller
with the ServiceController constructor:

ServiceController sc = new ServiceController(name);

sc.Start();

best regards,
Henning Krause
 
G

Guest

Great, thanks!

And what about this one:

controller does not contain Startup Type, Log On As info. Any other .net
module may provide those info?
 
G

Guest

Thank you, Henning, really appreciate.

I received one suggestion for my previous question: Is that possible for a
Windows service to have public properties/methods so that other assembly may
access it?

In the assembly, say a .exe app, that needs to access this Windows service's
properties/methods/events, just reference this service either at design time
or at run time, and treat this service as any other module assembly.

What is your comment on this approach?

Thanks.


Henning Krause said:
Hello,
Also, controller does not contain Startup Type, Log On As info. Any
other
.net module may provide those info?


for this info, you must use the Win32 function QueryServiceConfig (See
http://www.pinvoke.net/default.aspx/advapi32/QueryServiceConfig.html for an
example.

Best regards,
Henning Krause
 
H

Henning Krause [MVP - Exchange]

Hello,

that's not possible due to process isolation. You'll have to stick to some
sort of interprocess communication.

Best regards,
Henning Krause

Andrew said:
Thank you, Henning, really appreciate.

I received one suggestion for my previous question: Is that possible for a
Windows service to have public properties/methods so that other assembly
may
access it?

In the assembly, say a .exe app, that needs to access this Windows
service's
properties/methods/events, just reference this service either at design
time
or at run time, and treat this service as any other module assembly.

What is your comment on this approach?

Thanks.


Henning Krause said:
Hello,
Also, controller does not contain Startup Type, Log On As info. Any
other
.net module may provide those info?


for this info, you must use the Win32 function QueryServiceConfig (See
http://www.pinvoke.net/default.aspx/advapi32/QueryServiceConfig.html for
an
example.

Best regards,
Henning Krause
 
G

Guest

But, since a windows service is a singleton, when one instance this windows
service, one will get a reference to this singleton. At this point, one may
access properties/methods, right?

Henning Krause said:
Hello,

that's not possible due to process isolation. You'll have to stick to some
sort of interprocess communication.

Best regards,
Henning Krause

Andrew said:
Thank you, Henning, really appreciate.

I received one suggestion for my previous question: Is that possible for a
Windows service to have public properties/methods so that other assembly
may
access it?

In the assembly, say a .exe app, that needs to access this Windows
service's
properties/methods/events, just reference this service either at design
time
or at run time, and treat this service as any other module assembly.

What is your comment on this approach?

Thanks.


Henning Krause said:
Hello,

Also, controller does not contain Startup Type, Log On As info. Any
other
.net module may provide those info?

for this info, you must use the Win32 function QueryServiceConfig (See
http://www.pinvoke.net/default.aspx/advapi32/QueryServiceConfig.html for
an
example.

Best regards,
Henning Krause

 
H

Henning Krause [MVP - Exchange]

Hello Andrew,

you can't get a direct reference to the service instance. Processes are
isolated from another in Windows. That's why there is inter-process
communcation.

Best regards,
Henning Krause


Andrew said:
But, since a windows service is a singleton, when one instance this
windows
service, one will get a reference to this singleton. At this point, one
may
access properties/methods, right?

Henning Krause said:
Hello,

that's not possible due to process isolation. You'll have to stick to
some
sort of interprocess communication.

Best regards,
Henning Krause

Andrew said:
Thank you, Henning, really appreciate.

I received one suggestion for my previous question: Is that possible
for a
Windows service to have public properties/methods so that other
assembly
may
access it?

In the assembly, say a .exe app, that needs to access this Windows
service's
properties/methods/events, just reference this service either at design
time
or at run time, and treat this service as any other module assembly.

What is your comment on this approach?

Thanks.


:

Hello,

Also, controller does not contain Startup Type, Log On As info.
Any
other
.net module may provide those info?

for this info, you must use the Win32 function QueryServiceConfig (See
http://www.pinvoke.net/default.aspx/advapi32/QueryServiceConfig.html
for
an
example.

Best regards,
Henning Krause

 

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