Why .NET adds extra quotes for service ImagePath when installing service

464 views Asked by At

I faced very strange behaviour of .NET when it is installing a system service.

I added 2 logging calls into OnBeforeInstall method. Here is what it shows just when it was entered method:

+ PreparingInstallation: 4 items in Parameters:
 * #0/name = TcpGw_7050
 * #1/assemblypath = D:\sandbox\x.NET\bin\Debug\TcpGwSrvc.exe
 * #2/logfile = C:\Users\Dmitry\AppData\Local\Temp\install-TcpGw_7050.log
 * #3/logtoconsole = 

Here is what it shows at the end of OnBeforeInstall method:

+ ReadyToInstall: 4 items in Parameters:
 * #0/name = TcpGw_7050
 * #1/assemblypath = "D:\sandbox\x.NET\bin\Debug\TcpGwSrvc.exe" TcpGw_7050
 * #2/logfile = C:\Users\Dmitry\AppData\Local\Temp\install-TcpGw_7050.log
 * #3/logtoconsole = 

But here is what was really installed for the ImagePath property of that service:

""D:\sandbox\x.NET\bin\Debug\TcpGwSrvc.exe" TcpGw_7050"

So, by unknown reasons it was enclosed whole string into extra double quotes! But why? And how to fix that?

I already tried to enclose parameter with double quotes:

 * #1/assemblypath = "D:\sandbox\x.NET\bin\Debug\TcpGwSrvc.exe" "TcpGw_7050"

That did not help.

I tried to not enclose anything with double quotes:

 * #1/assemblypath = D:\sandbox\x.NET\bin\Debug\TcpGwSrvc.exe TcpGw_7050

That also did not help.

Why it does matter: because when ImagePath contains such extra double quotes I cannot start service because of error:

System error 87 has occurred.
The parameter is incorrect.

I tried to goodle this problem but not found any explanations for that.

Also a kind of rethoric question - why there is such conceptual gap in the .NET framework design that it does not allow to specify service StartParameters using some standard way? So, programmers have to use that trick with "AssemblyPath" parameter in Context.

Thank you in advance.

PS. It is .NET 3.5 service application. Adding service StartParameter - that is done by me in a code. The question is not in adding StartParameter itself. The question is - why .NET framework adding extra double quotes there? And how to avoid that?

PPS. Yes, I know that I can write a piece of code that will open a registry key and modify an ImagePath value but I would like to avoid using such dirty methods, if possible I would like to do all with standard & simple features of .NET ServiceInstaller. Is it possible at all?

Workaround: I implemented a trick - added one quote after assemblypath and one quote before parameter, so now at the end of OnBeforeInstall method in a log output I see following:

 * #1/assemblypath = D:\sandbox\x.NET\bin\Debug\TcpGwSrvc.exe" "TcpGw_7050

With an idea that after .NET will add quotes around such ImagePath value it will became a correct value of exe + parameters. As I can see this workaround works fine. But I'm worying - what if this bug with extra quotes in .NET will be fixed in future?

0

There are 0 answers