views:

59

answers:

3

I wrote a C# Windows Service to handle task scheduling for our application. I'm trying to move the "business rules" assemblies into a bin subdirectory of the scheduling application to make it easier for us to do updates (stop service, delete all files in bin folder, replace with new ones, start service).

I added

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
     <probing privatePath="bin;"/>
  </assemblyBinding>
</runtime>

to the service's app config and it works fine if the service is run as a console application. Problem is when the service is run as a windows service it doesn't work. It appears that when windows runs the service the app config file gets read properly but then the service is executed as if it was in c:\windows\system32 and not the actually EXE location and that gums up the works.

We've got a lot of assemblies so I really don't want to use the GAC or <codeBase>. Is it possible to have the EXE change it's base directory back to where it should be when it's run as a service?

A: 

Use a fully qualified path. You are right about it looking for the bin in system32 if you don't fully qualify it when it is running as a service.

Tom Cabanski
I didn't think you could do a fully qualified path with privatePath, that it had to be a subdirectory of the EXE's directory. Or is my knowledge outdated?
SauerC
Duh. Forgot about that. Ignore my incorrect answer!
Tom Cabanski
A: 

I think the differences you see when running from the command prompt or when running as a service are due to the fact that the application is running in a different security context. When running as a service it's probably using the LocalService or LocalSystem account. Otherwise the application runs with the account you logged on to PC.

You may try running the service using the same account you use to log on to the PC (for the sake of troubleshooting the problem). See if this fixes the problem. If it does, you may think about creating a user account specifically for your service. Another fix may be to set the base directory associated with the account you use to run the service to your application directory.

I know this is all a bit of guesswork but maybe it can help you get closer to finding the root of the problem.

Seventh Element
A: 

Turns out I was wrong. privatePath works fine even from a windows service. Issue was I had added the <runtime> section to the top of the app.config which throws an error because <configSections> is not the first item in the file. However the service still signaled that it started fine (something we have to fix) and the log file used for start up errors was being written to a directory other than the one with the EXE (something ELSE we have to fix) once I moved the <runtime> block to its proper place in the app.config everything worked perfectly.

Kudos to the comment for the fuslogvw.exe, I'd give the answer to you if I could. It allowed me to see that the privatePath was being followed and that the error was elsewhere.

SauerC