5

I have created a Windows service to manage some files for me. For debugging purposes I wrote a console entry point as well so all my code for the actual service is an a separate class from the windows service. I am using a SQLite DB with fluent NHibernate to store settings etc.

When I run the console mode, it works perfectly, but when trying to start the process it doesn't. I have done a debug on start with my process and found that when NHibernate tries to create the session, it can't see the db file even though it does exist.

I have setup the process to run as an administrator, and have also tried running it as the local system account with "Allow access to desktop" enabled, but it still won't pick up the DB file. This means that NHibernate can't load the file so it creates a new one, and when my service tries to load data from the DB it fails since it has a blank DB File loaded.

NoWar
  • 36,338
  • 80
  • 323
  • 498
MrThirsty
  • 343
  • 2
  • 12
  • Is the config file in the right location? Is there a permission problem, e.g., running as admin from the console and local user in the service? – Matt Davis Jan 08 '13 at 19:19
  • 1
    Did you check as which user the service runs? By default services run with a special service user (you can specify a different one though). If it runs as service user, that user may simply have not enough access rights on the file. – Zarat Jan 08 '13 at 19:56
  • Don't check "allow access to desktop". First step is to tell us what user the service runs as. – David Heffernan Jan 08 '13 at 20:36

2 Answers2

4

So it turns out that even though my process files where in a specific folder, it was running under the context of the Windows directory, I have added the following code in and now it works perfectly:

System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);
MrThirsty
  • 343
  • 2
  • 12
  • Nice. You just saved my head with a partially working service that worked perfectly as a standalone exe in another sln!!!! :) One of my app.config settings is for a file used by an API i connect to (thus I cannot control what it's doing). I had tried programmatically setting the currentDir path for this but it didn't work. This line worked like a charm and will be nice for deployment too (and handing over to a support team) – TilleyTech Jun 05 '19 at 15:19
1

It could be a file permissions problem. Check that the service executing user can read/write the db file.

Alternatively, it can be a working directory issue: What directory does a Windows Service run in?

You can use absolute paths to find the file.

You could use SetCurrentDirectory() early when starting you service, passing it something extracted from the path of the service executable. On the other hand, normally modified data should not be in the installation directory of the service, but e.g. under ProgramData folder instead.

The app.config is still loaded as usual, so this can be used to configure any installation dependent paths as needed.

Community
  • 1
  • 1
Oskar Berggren
  • 5,583
  • 1
  • 19
  • 36
  • The user I have set the process to run as is a Local administrator who is also the owner of the DB file. The process files location is the same as the process itself. The process and command line are being run exactly the same way with the same user, the only difference between the two is that the process can't see the DB file while the console mode can. – MrThirsty Jan 10 '13 at 06:53