Creating a long-running server for other windows applications
I've looked at COM Servers and Windows Services, but I'm not sure what works best for my purposes or how to go about it. I want something that can be started and run indefinitely, which contains an object, so that other processes or applications get references to that object (or make requests to the server) to change or query its state.
A server is an application that processes commands for a device over a serial port and maintains the internal state of the device.
I have a device communication and persistence function written in C # right now and can be instantiated and run based on each process, but obviously I want it to instantiate once and run independently of other processes.
The COM tutorials I read only confused me as I wasn't completely sure if this was what I wanted and I was hoping there would be more .Net way to do it.
Any help would be much appreciated!
a source to share
I would build a Windows Communication Foundation (WCF) service for this and expose it using one of the many bundles available. You can view it via HTTP / SOAP, HTTP / REST, TCP / Binary, MSMQ, etc. All from one service. If you have Windows 2008, you can activate it through Windows Activation Service (WAS). If you have another Windows OS, you must write a Windows service that you can register with the Service Control Manager (SCM). There are many options here, but I am definitely leaning towards using WCF to create your service. There are so many "plumbers" that WCF takes care of you. You don't have to invent all this. Hope this helps.
a source to share
Your requirements are a bit conflicting - if a server is to run indefinitely, whether there are clients to serve or not, and the process should be common to all clients, no matter where they run, Windows Services is the way to go.
If you only want the server to run when there are clients and perhaps (or not) so that those clients can use the same server process, COM Out Of Process Servers might be an option.
In the case of COM, you will have to use DCOM to communicate with the server process. For services, you can use DCOM, Named Pipes, RPC, or some other IPC mechanism.
If you want to code a server in C #, however, DCOM seems like a bit of a strange choice - it is possible to create DCOM servers in C #, but this is really awkward. As Kevin pointed out, WCF / WAS / Remoting might be an easier choice. But keep in mind that such a solution will almost certainly have significantly higher wrt memory overhead than a native COM server or service. If this piece of software is going to be installed on client machines, I would prefer my own solution.
a source to share
You seem to be asking for two things -
- a way to host a long-running, persistent application on Windows that will play on some device or serial port.
- a way to link to this app from other apps.
Windows Service is a great solution for the former. The Windows service acts as an application host for your custom logic. It gives you start / stop and easy monitoring and customization through a common interface and API. It's easy to create Windows services in C #. Easily start and stop Windows services from the command line, from an application, from a GUI tool.
Think of WCF as your main communication interface. WCF can be used to build a front end that is exposed by a Windows service. If you want to connect to an application (service) from .NET or HTTP (REST or SOAP) clients then WCF will work for you.
COM .... The confusion you run into where COM resides is because COM connects the interface mechanism (IDispatch, IUnknown, etc.) to the hosting + lifecycle model (local servers, Inproc servers, Remote servers , automatic start on demand, etc.). Hosting and lifecycle in COM worked, but it was always pretty thin. It is difficult to know which services were installed and for how long. It is difficult to start and stop on demand.
COM, in this situation, like WCF, is primarily of interest as a communication interface. If you want to use a Component Interface to connect to a service, COM can be a good addition to a Windows hosting service.
COM and WCF (REST or SOAP) are not mutually exclusive, and in fact many applications provide multiple interfaces. You can choose one or the other, or both. There is no wrong answer, it depends on your requirements.
If I was doing this, I would be using a Windows service, and I would also provide a COM interface from that service, as described here . The lifecycle and hosting service is served by a Windows service. The service is then available via COM, which means just about any programming or script language. Javascript, VB6, Excel or OFfice, Python, Perl, etc.
I would also consider exporting the WCF interface from the service. This will allow any REST client to connect as well. Think of WCF as a cordless phone and COM as a landline phone. Sometimes you only need one. Sometimes you need both.
a source to share