简体   繁体   中英

C# Windows Service doesn't seem to like privatePath

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM