Thursday 7 April 2011

Moving a C# Windows Service to Windows 7

Problems moving a Windows Service to Windows 7

I have just been given a new PC at work that runs 64bit Windows 7. I have to say so far I quite impressed with the OS and the great thing about Windows 7 is that I can now install IE 9.0 which is blindingly fast and they have finally seen the light and made it standards compliant with support for the DOM 2 event model.

One of the jobs I struggled with today though was making a Windows Service that I had created on my old XP machine in Visual Studio 2010 run on my new box.

The service is basically a BOT that makes SOAP requests behind the scenes to a 3rd party server.

The code was working fine as I had moved it all from a standalone console application that worked but whenever my service harness EXE tried to run I would just get bland _COMPlusExceptionCode errors and nothing of any meaning in the event log.

Debugging a Windows Service was a right pain in the behind and after many uninstalls, installs and lots of shouting I finally got it working.

These steps might not work for everyone but I had to do all of the following to get my service working.

1. Access Rights. Even though I supposedly had administrative rights on my PC I couldn't even install the service with the installUtil [path to exe] command without having to right click on the correct Visual Studio command prompt and choosing "Run as administrator".

However even after doing this I was still having problems starting the service and I was getting the standard

The service did not respond to the start or control request in a timely fashion.

Error message whenever I tried starting the service from the service administrative tool or from my desktop wrapper application.

After some step through debugging I found an "Access Denied" error message was occurring on the ServiceController.Start() method call so I went into the User Account Control Settings and turned the scrolling control right down to "Never Notify". As I wasn't allowed the logon details for the Local System account I was using my own login details for the service as this seemed to be the only way to get round this error message with a reboot.

2. Rebuilding the Service as an x64 solution and not x86. I tried this due to a weird win32 error I was seeing and just put 2 and 2 together and presumed that something was not being run correctly due to it now being on a 64 bit machine.

3. Adding the following code to the config file:

<runtime>
<generatePublisherEvidence enabled="false"/>
</runtime>


I obtained this clue from the following site: stackoverflow.com and it seemed to be the final part of the jigsaw that finally got my service and the wrapper application working on my new machine.

I am fully aware that I am not describing all the reasons behind the decisions I made but I am not exactly aware of the whys and hows at this point in time. However if I come up with further answers I will let you know. Plus if anyone else has any useful links or tips about this issue please add them to the comment section.

2 comments:

Mike Niller said...

Hello,

i want to say thank you for a great job you've done on your blog.
I have a software download website and I also write articles for people to help them with their computers and software. Is it possible to place this article on your blog as a guest post?

Regards,
Andy G.

Rob Reid said...

I am confused.

Are you asking whether you want to put this article of mine on YOUR blog or are you asking to put an article on MY blog?

Your comment suggests the latter but you haven't provided me with a link to the article so I cannot answer you until I have read the article in question.

If it's a case of you mixing up your words and you are requesting the former then yes feel free to re-post this article.

As long as its properly attributed to myself with a backlink to the original source then yes go ahead with my full blessing :)