Windows services does not run my class method - c#

I have a windows service application which would scan the wifi list and save to file. then i will read the file and write to mysql database and close the file. my wifi scanning and db methods in another class within same windows service project which i invoke in service class. I am able to run and install the windows service but i think those methods are not called. I have even tried them in the service class but they still didn't call up in OnStart method. When i run it section mode and debug from visual studio it works fine but when i install it in installutil it only starts the service nothing happens though the service still runs in the background. I've created windows form version of this and that works fine.

I think it's more likely that the class method is called but writing the output file fails. The account that is used to run the service likely has no rights to create and/or write the output file. Check the folder that it should be written to and see if the account that runs your service has proper access rights. You can also use Process Monitor to verify this (set a filter on Path or Process Name).

Related

How can I create a Windows Service which updates itself without user interaction

I have built a C#, .NET3.5, web application that installs a Windows service (run as the NetworkService user). This service acts as a specialised web server for any machine on the network, and the user connects to the service using their browser.
The whole thing is installed using an msi file built using Visual Studio 2008. The msi file installs and runs a deployment app, which is what deals with setting up users and permissions, and installing the exe as a service.
If I decide to enhance the app (or find and fix a bug), I can produce a new msi file, and get the user to run it on all the machines running my app. But I would rather that this happened automatically (including downloading the updates from my web site, and deploying them).
Because it runs as a service, it is not user-specific, so I understand Click Once deployment is not an option.
Is there any way for me to achieve this?
If you really want no user interaction at all in the process, could you have a second service that is the update watchdog and identifies when a new version of the main service is available, and then handles the download/stop/install/start of the main service?
Here is the solution I ended up with:
When installing the service, I gave the service user permission to start and stop the service. See my question on service permissions for more details.
The service program checks a remote url for a version file, and checks it against the current program version. If the version file is different, it downloads an update zip, extracts the update program from it, locks a file, runs the update program and immediately exits.
The update program locks the same file, waits a few seconds (just to make sure the service has stopped), unzips the zip (excluding the update program itself), and finally restarts the service.
Here is the solution I am trying now:
There is a stub service, which does nothing else but check for an update program, run the update program, then run the main service program. If the main service program exits with a particular exit code, it loops round and repeats (otherwise it exits normally).
The main service program checks a remote url for a version file, and checks it against the current program version. If the version file is different, it downloads an update zip, extracts the update program from it, and exits with the special exit code.
The original installer gives the user account under which the service runs full permission to the program folder (so the update program can update the service program).
The only drawback is that it is impossible to update the stub service.

Installing a windows service as another user than Network Service

I am using a self-installing variant to install my windows service. That means I run the exe that makes up the service, and when run it installs itself using the following line of code:
ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
This works fine, and the service gets added to the list of services. My only problem is that this service gets added with Log On As equal to Network Service. I am doing certain operations from this service that require it to log on as Local System.
How can I make it install itself to run as Local System instead of Network Service?
I think you are using ServiceProcessInstaller. If so, you can set ServiceAccount.LocalSystem to its Account property

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 start process from ASP.NET Web Service (and allow to it to do everything what it wants to do)?

There is a Web Service installed on Amazon Server. Exposed WebMethod should start an executable. But, it seems that process (executable) started by WebMethod has not permissions to finish its job. If a WebMethod is called locally (using IE on Amazon VM) I can trace some events into log file placed on the path: C:\\LogFiles. But, if it is called from remote machine, there is no even log files. Locally, on my machine all works fine.
The question: is there any way or settings in IIS7 to allow to my WebService to create process that can do everything I want to do? In web.config file I added a line:
<identity impersonate="true" userName="USERNAME" password="password"/>
(userName and password are, of course, written correctly in the file).
Also, I tried to use impersonization as it is explained here, but there is no result. My process can't do its job, it cannot even trace actions into log file. Locally, on my machine, everything works fine.
Any idea how to change settings or whatever into IIS7?
EDIT: In addition to the main question: my WebService is not able even to create log files on the path: C:\\LogFiles - although it is able if it started locally, but remotely there is no simple log file that contains some string. How to allow it to create simple text files?
If all else fails, you may start such a process separately and make it wait for a signal. You can supply a signal in many ways — via an IP socket, via a mailslot, via a named pipe. The web service will post requests to the command pipe (or queue), and the 'executor' process will pop commands, execute them, and wait for more commands.
You should avoid trying to start external processes from ASP.NET - if anything, because your application will then be running under the context of the ASP.NET account. (Yes, you could use impersonation to launch into another account, but, lets not go there)
Instead, install a Windows Service which can receive a signal* to launch the executable you wish.
This has the advantage that you can customise what account the service runs under, without putting passwords inside your code.
(*) Signalling could be achieved through a number of means:
WCF Service Call (using a WCF Service being hosted by the Windows service)
Monitoring for a filesystem change to a known directory.
If you were using Linux, I would have given you the smartest solution ever, setting SUID flag, which is not possible in Windows.
The problem with impersonation is that it works as soon as you have some control over the server machine, more than having appropriate credentials.
You mentioned Amazon VM: I'm pretty certain that they won't allow, for security reasons, to perfrom impersonation. [Add] Or, better, they won't allow anybody to write in C:\
Option 1
Switch to Mono/Linux, set SUID bit using chmod from console and rock!!
Option 2
If you can run the executable other way than ASP.NET (ie. you have a Remote Desktop, SSH*) as privileged account (note: privileged doesn't mean Administrator) then you can redesign your application to have ASP.NET invoke services from your daemon process using WCF, Web Services or Remoting. But, in this case, you have to redesign your executable to be a stand-alone server.
[Add] None of the solution fix if your hosting provider doesn't allow you to write in paths such as C:\, but only allows you to write under your home directory.
*It works on Windows too!!!! And I mean the server!!!

Categories