2

Ok, so I know how to write both a service and a background application, I'm looking more for general opinions because this is an odd use-case in the sense that I haven't been able to find examples of this online to give me guidance.

Basically the issue I am running into is I have a server (an application, not a web server) that houses all my application logic, and a set of clients that can send messages to the server to do things.

The clients, obviously, are traditional UI applications. Problem is what the server should be...

It doesn't need a GUI, and when it is started, there should be only one version of it on the machine. On the other hand, it should only be running if there is atleast one client, otherwise it should shut off.

I may be over-thinking it, but should I make this a service or a background application that is instantiated by the first client?

I am referencing things like: Windows Service vs Windows Application - Best Practice

Community
  • 1
  • 1
Gauss
  • 43
  • 6
  • Internet or intranet based? What are the security requirements? – emd Jun 10 '13 at 19:38
  • 1
    Why must it turn off if there are no clients? How will it wait for clients to connect? – Matt Houser Jun 10 '13 at 19:41
  • What will this processor be doing? – Matt Houser Jun 10 '13 at 19:42
  • Can you see ["installig a console application as a windows service in windows server 2003"](http://stackoverflow.com/a/16904087/1958630) thread. – ramazanulucay Jun 10 '13 at 19:54
  • @MattHouser - It should turn off when there are no clients because it serves no purpose, its not a web-application its just an application. Its the central repository for all the logic and how the data changes based on user commands. If there is no user to command it, no point in having a service to interact with the data. So, as an example, there is data (state of some property) and several clients may want to query and change that. The service would handle the validation and data passing, but if no client is there, the data can just be saved off and the server shut off (to save resources) – Gauss Jun 10 '13 at 20:25
  • 2
    When there are no clients, it does serve a purpose: to wait for the next client to connect. You're over-estimating the resources required by an idle windows service. A service waiting on a socket basically takes next-to-no resources: only the socket and a modest amount of memory (if you create it correctly). – Matt Houser Jun 10 '13 at 20:33
  • @MattHouser: Microsoft's advice has always been to shut down services that don't need to be running. One idle service may not have much impact, but they add up. – Harry Johnston Jun 14 '13 at 02:28
  • @HarryJohnston I'm not disagreeing. But if a server needs to be listening for client connections (such as a web server, database server, etc.), then some process needs to be running to actually do the listening. You need to weigh the effort involved in launching your server "as-needed" compared to the amount of resources required to keep an idle service running. – Matt Houser Jun 14 '13 at 17:22

3 Answers3

2

Use a Windows Service. It'll start when your server starts and keep running until the server is shut down.

There's no need to turn off when there are no clients. If the application is doing nothing but waiting on a socket for a new connection, then it'll occupy a very small amount of resources: the socket and some memory.

Besides, if you turn off your service, what mechanism are you going to use to restart it when a client wants to connect?

Matt Houser
  • 33,983
  • 6
  • 70
  • 88
  • The same way I'd start a Daemon in Linux... I'd imagine there has to be some protocol the client can use to query if the service is run and request/restart it. I could be wrong, I am somewhat new to services as I typically don't code them. The problem is the qualifications that fell out of my problem seemed to point to MAYBE a service is a better route than a background application. Its one big data repository and logic center to handle validation and lots of clients making requests (I'm trying to avoid multiple clients interacting with/writing to the same files, I want a "gatekeeper") – Gauss Jun 10 '13 at 20:49
  • @Gauss - In theory you can remotely start a service on a server, but then you're giving a lot of responsibility to the client application. Unless you can give good reason for the service to shut down you're looking at a lot of overheard for nothing. – Logarr Jun 10 '13 at 20:50
  • @Logarr Maybe, but there is no remote, this isn't a webserver. Client and Server are local to the user's machine, I use those terms because it follows that pattern ("Client" is interacting with data not local to its application space, it sends messages via a protocol to a "Server" which determines if that is a valid action and has sole responsibility on modifying data to prevent Clients from overwriting each-other), but I am thinking maybe I choose the wrong terms... – Gauss Jun 10 '13 at 20:56
  • @Gauss - Your question made it seem like you were running a server on an internal network. If you're running all this on the client machine, and you want the service to stop when the client is closed, I'm failing to see why you don't just make a single piece application. – Logarr Jun 10 '13 at 20:58
  • @Logarr - Because the client may open up multiple UIs to simulate multiple interaction points/instances, and different clients may serve different purposes. For example, there are 4 versions of the display client right now. I don't want to make 1 gigantic, unwieldy application, specifically because the people I am delivering to will divide this up (into 4 versions now). Also, the clients interact with one piece of data that houses the current state of the simulated system. I don't want to risk those clients having conflicting information. – Gauss Jun 10 '13 at 21:11
1

I myself had a similar problem and I solved it by writing a Windows Service which was a self-hosted Web API app too.

For instructions see my answer here.

It's really efficient, unlike self-hosted WCF, it does not crashed from time to time and it provides you a standard interface: HTTP, JSON (or XML if you prefer) and (if you need it; you can employ) REST.

Community
  • 1
  • 1
Kaveh Shahbazian
  • 13,088
  • 13
  • 80
  • 139
0

Use a Windows Service, so that the server is always running in a consistent context.

Configure the service so that the clients can launch it as needed:

wchar_t sddl[] = L"D:"
    L"(A;;CCLCSWRPWPDTLOCRRC;;;SY)"           // default permissions for local system
    L"(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)"   // default permissions for administrators
    L"(A;;CCLCSWLOCRRC;;;AU)"                 // default permissions for authenticated users
    L"(A;;CCLCSWRPWPDTLOCRRC;;;PU)"           // default permissions for power users
    L"(A;;RP;;;IU)"                           // added permission: start service for interactive users
    ;

PSECURITY_DESCRIPTOR sd;

ConvertStringSecurityDescriptorToSecurityDescriptor(sddl, SDDL_REVISION_1, &sd, NULL);

SetServiceObjectSecurity(service, DACL_SECURITY_INFORMATION, sd);

You may want to adjust the ACL slightly to meet your precise needs. Note that error checking has been excluded for simplicity.

The clients can use the StartService function to launch the service as required.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158