Configurable Windows Service - How and where to store configuration - c#

I created a C# application to manage data synchronization between an ERP and a CRM. This application reads a table every 500ms, and sends data via WebService to CRM.
For the moment, I have two screens for my application : a first to configure connections (ERP's DB connection informations and CRM's URL and WS Token), a second with a start button to launch the loop thread.
I know want my application to be nothing more than a windows service installer. What I want to do is to launch my application, configure ERP and CRM connections, and then click on a 'Install' button. When this button is clicked, a service is created and makes the same work that my application makes now when i click the 'start' button.
I already read those links Converting my application in a Windows Service, Making an existing exe with gui into windows service and of course http://msdn.microsoft.com/en-us/library/d56de412%28v=vs.100%29.aspx.
Here is my question :
How to make a configurable windows service ? I think the simplest way is to store configuration into files that utility and service would share. Where to store those files ? The utility must be able to find these files afterwards : registry ?
Thanks,

It's a common practice in .NET to store application's or service's settings in this relative path Environment.SpecialFolder.ApplicationData

Related

Managing Windows Service (creating, deleting, starting, stopping, arguments)

What I am trying to do,
i have an executable file (myService.exe). it is a windows service and it supports start, stop and other such options. It uses one argument (filename) and run its survices on it.
e.g. c:\myService.exe someFileName.dat
now i am making a c# windows form application. I am reading filename from a textbox and on button click event i want to do this.
check whether myServices.exe is installed on machine or not.
if it is installed then i have to check it is in running state or not.
if it is in running state then stop it.
after stopping it i need to uninstall the service.
after uninstalling it i want to install it again and have to give filename as the parameter of service and then run it.
How to install a windows service programmatically in C#?
i have tried this but i have to give argument to the service which is not present there.
Why not create a List that the windows application uses or if you are stuck on using a Windows Service application have a .config file that the windows service app uses and store the initial directory in it. using the FileInfo and DirectoryInfo and of GetAllFiles() method store the files in a List and iterate the List varList = new List(); variable object that way.
You could also make a Web Service and have the windows application consume that web service by adding a web reference the same way you would if you wanted to add a reference to an additional / 3rd party assembly. Lots of options you have

c# windows service process.start

I did create a windows service. Can I add in the OnStart(string[]args] method the Process.Start - used to open another executable. Will it work? I want to create an windows service application that opens an executable file (foor example cmd where i can add my own commands to open a notepad file . Thx
You would have to allow the service to interact with the desktop (under service properties in Services mgr), but is sometimes considered a bad practice. Note, this is made difficult in Windows 2008 and above because of Session 0 isolation.
It works, but the rights the process run, as well as the resources it can see are the same as the wiundow service ( ie Local System Account ) if you don't change the defaults. This could make the difference or not, depends on what the executable do. If the executable has to show some UI you will experience troubles since by default the servcie does not see the user desktop. The same happen if you need to see a mapped network drive. In general it works if the executable is some batch that does not requires th econnected user profile right/resources.

How to do things within a windows service?

I have an application, written by me in C#.net 2.0 - when the application is opened, a timer will check every 3 seconds for x. If x happens, it shows a warning in a windows form.
Is there a possibility to install this timer and the windows form call in a windows-service? So that the timer ticks every time a system is up and shows the message then?
No, it is not possible to have a service display a form. This is by design, since a service is supposed to run without a user interface
You can have a regular application that communicates with your service and displays the warning, but I don't know how exactly this is done.
IMO, you don't need a service, just create a regular application without a main form that runs in the background, performs your check, and displays a warning when necessary. You can add that application to the Run section of HKLM or HKCU, so that it is always started when a user logs on to the system.
Windows Forms cannot be displayed from a Windows Service. A Service runs only in the background.
You can have a separate Windows application that communicates with the Windows service and display warnings, information, etc.
To do this, the service runs on the LocalSystem account, and you have to enable the property for the service to interact with the desktop.
Services are forbidden from interacting with the desktop, including displaying windows or forms. In windows 2003 and XP you could work around the issue by marking the service 'interactive' and this would allow the service to display on the user session, but as of Windows 2008 and Vista this is enforced and services can no longer interact in any fashion with the user.
When services need to display anything, the solution is to split the logic into two separate processes, one being the service and one being a normal user process. The user process runs launched by the user, in its session (it can be launched automatically at session start up) and it connects to the service via some IPC channel (shared memory, named pipes, sockets etc). The service can then display anything it wishes to the user by asking the user process half of the application to display what it needs to display.
As others have said, remember that a windows service is supposed to be a background, never-interacting-with-the-user program. Many services run even when the user is not logged on -- how would they go about displaying a form when there's no desktop for them to display it on?
That said, it shounds like you're trying to shoehorn something into a service that shouldn't be a service. If you want something that runs in the background and still interacts with the user, look into making a lightweight system try application. In fact... here's a helpful tutorial:
Lightweight System Tray Application (NotifyIcon Based)

Run an exe when machine starts

I have created an SMS application in .NET.
I wanted that the application should run when the computer starts, even before the user logs in.
Just like the SQL Server.
You need to create your application as a Windows Service. The linked MSDN page will provide full details.
Microsoft Windows services, formerly known as NT services, enable you to create long-running executable applications that run in their own Windows sessions. These services can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface. These features make services ideal for use on a server or whenever you need long-running functionality that does not interfere with other users who are working on the same computer. You can also run services in the security context of a specific user account that is different from the logged-on user or the default computer account. For more information about services and Windows sessions, see the About Services section in the Platform SDK documentation in the MSDN Library.
You could consider making it a Windows Service.
You'll have to write a service. Start here...
Wrap your application in a Windows Service, using the .NET System.ServiceProcess namespace.
The System.ServiceProcess namespace provides classes that allow you to implement, install, and control Windows service applications. Services are long-running executables that run without a user interface. Implementing a service involves inheriting from the ServiceBase class and defining specific behavior to process when start, stop, pause, and continue commands are passed in, as well as custom behavior and actions to take when the system shuts down.
Use ServiceEx to make a service from your executable: http://serviceex.com/ , just write one INI file, you can choose if application's window is hidden or showed etc.

C# Windows Form with Attached Windows Service

I know how I can create a Windows Service in C# but I have a series of variables that the user currently feeds into a text file and I wanted to control this process. So can I attach a Windows service to a Windows form application to control such things as installing the service, uninstalling the service, start and restart. Or is it better to have them completely separate and just use the form app to run system commands for install/uninstall and use the serviceController to start/restart?
Thanks
I suggest to make it completely separated.
It's probably better to keep them separate: have the form write a config file for the service, then either restart the service or have the service detect the changed file and reconfigure itself.
If the service interacts with the desktop, then you'll have to start worrying about what happens if they're running as different users, or if there isn't a logged-on user at all, or...
You can do it by allowing the service to interact with the desktop. It's an acceptable way of doing it, but saying that, I agree with dario-g!

Categories