Converting Windows C# Application to a Windows NT Service

G

Guest

Hey guys,

I've written a fairly simple Windows C# Application using Visual Studio
2005. I have to manually run it, but I would prefer to have it run on it's
own, as if it was a Windows NT Service. I am a Delphi convert (still use it
for some jobs). When developing in Delphi, I would write a console
application that would basically run on it's own. In order for me to register
it as a Windows NT Service, I seem to recall that I would do something like
SVCREG32.EXE (Application.EXE) /REGISTER or something to that effect. It
would then appear under my list of Services. I could control the startup of
the application.

In my Delphi application itself, I would use a timer which would make the
app run once every... I dunno 2 minutes or something?

With C#, I don't have that. I haven't actually checked to see if there is a
timer component, but I'm sure I can do something. My real question is though,
how can I register my Windows C# Console Application as a Windows NT Service?
Does anyone have any examples of how to accomplish this?


Thanks!
 
T

Thomas T. Veldhouse

Todd Jaspers said:
With C#, I don't have that. I haven't actually checked to see if there is a
timer component, but I'm sure I can do something. My real question is though,
how can I register my Windows C# Console Application as a Windows NT Service?
Does anyone have any examples of how to accomplish this?

I don't think that you really want a service to be just any old console app.
Take a look at this link for reference.

http://msdn2.microsoft.com/en-us/library/y817hyb6.aspx
 
M

Michael Nemtsev

Hello Todd,

Creating services with UI isn't a good approach due to security reasons,
because your service works in different context ragher then user and service
may start even when no user logged on

Just create service apps like Thomas recomended u

TJ> Hey guys,
TJ>
TJ> I've written a fairly simple Windows C# Application using
TJ> Visual Studio 2005. I have to manually run it, but I would prefer to
TJ> have it run on it's own, as if it was a Windows NT Service. I am a
TJ> Delphi convert (still use it for some jobs). When developing in
TJ> Delphi, I would write a console application that would basically run
TJ> on it's own. In order for me to register it as a Windows NT Service,
TJ> I seem to recall that I would do something like SVCREG32.EXE
TJ> (Application.EXE) /REGISTER or something to that effect. It would
TJ> then appear under my list of Services. I could control the startup
TJ> of the application.
TJ>
TJ> In my Delphi application itself, I would use a timer which would
TJ> make the app run once every... I dunno 2 minutes or something?
TJ>
TJ> With C#, I don't have that. I haven't actually checked to see if
TJ> there is a timer component, but I'm sure I can do something. My real
TJ> question is though, how can I register my Windows C# Console
TJ> Application as a Windows NT Service? Does anyone have any examples
TJ> of how to accomplish this?
TJ>
TJ> Thanks!
TJ>
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
G

Guest

Michael Nemtsev said:
Hello Todd,

Creating services with UI isn't a good approach due to security reasons,
because your service works in different context ragher then user and service
may start even when no user logged on

Just create service apps like Thomas recomended u


Thanks Guys... Ill take a look at the link. I think I might not have made
myself very clear, but there would be no UI in this application. Although
it's just a windows app now, I would convert it to a console app with no UI
interface at all. I would actually want it to run on startup even if no user
logged in. It would be pulling data from one SQL database and transmitting
data through an SDK.


Thanks!
 
G

Guest

Oh ok!!!

I just read the link... I'm still new to VS 2005 (or VS in general) so I
completely missed the fact that there is a TYPE of project I can create from
scratch that's already meant as a service.

I'll just create a new app and copy my code from my console app into it.


Thanks guys!
 
E

EmeraldShield

Using a CLI app with a timer is not a service. You can't start / stop /
pause it etc.

Dot Net has very powerful abilities to build services. You start them,
stop, pause have them auto start, etc. It is very easy to do.
You do write them like a console app, but you start them differently.

You need a servicebase - look it up in MSDN and it gives a pretty good
example including how to write the Install class correctly.
 
K

Kevin Spencer

Something else you might want to consider: separating your business logic
from your interface logic. That is, create a business class that performs
the actual work, but only presents a programming interface. Then you can
host it in any kind of service or UI that you want in the future.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

If the Truth hurts, wear it.
 
G

Guest

Shoot... I have VS 2005 Standard... everywhere I look it says the Service
components aren't supported because I don't have Professional or higher... :(

Do I have any other option other than to ask my Boss to buy the Professional
version?


Thanks!

Todd
 
M

MarkusR

Todd,

I am using VS2005 team Suite that has a 180 day trial. Maybe if you
impress your boss after 180 days he may pay for it. :)

I have done exactly what you are asking about. I too am from delphi and
just wrote 6 services.

Like someone said above, keep the business logic seperate. I have both
a GUI interface and a service. They both call a shared unit.

basically, I create 2 projects and put them in the same root. Then I
create a common directory. You would then add the shared unit. Make
sure when you add that you click the little down arrow key on the add
dialog and chose to add a link. That way you don't have two different
copies. Why MS made that arrow so small an unobvious is beyond me.

This way I can test and watch debug info to the screen on the GUI app
and then launch it as a service when I am satisfied.

Hint, use system timers not windows form timers.

Any questions feel free to ask. I have learned a lot in the last 45
days. No doubt, there is always a better way but i will keep learning.

-Markus_R
 

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