Solved

How to compile a service writen in C# ( .Net 2.0 ) , uninstall and reinstall too

Posted on 2012-03-25
2
252 Views
Last Modified: 2012-06-22
The question may be simple to some but, I am not in a position to do trial and errors due to lack of a proper test environment.

This is someone else’s sourcecode we have inherited.
I have been given the task of recompiling ( afer some modifications ) and reinstall a newer version of a windows service that was written on .Net 2 C#

The current version of the service runs on a Windows 2003 Enterprise edition machine.

I need to do the following without a hitch - as the resulting deployment will go straight into the production - pls don’t ask me why no testing.

1. Compile the new code
2. Identify what parts I need to deploy - there is a ProjectInstaller.cs but does not seem to have much code - so I assume the previous people did a manual install ( see attached code for that )
3. Uninstall the currently running service
4. Install the new service

If you can provide some clear instructions , that would be much the better rather than me having to read article links.

Thanks very much,
InstallerCode.txt
0
Comment
Question by:indikad
2 Comments
 
LVL 22

Accepted Solution

by:
ambience earned 500 total points
ID: 37766849
To identify what parts to deploy:

Most likely only the service exe should be copied (unless all other assemblies are also recompiled, say for newer framework). A simple way is to Clean and Rebuld the solution. All assemblies copied to output folder must be copied over. (Also do a manual review and see whether any referenced assemblies are 3rd party and are CopyLocal is set to False, which is when they are being used from GAC). In that case, the 3rd party must also be installed - but since the service is already running just recompiling the service process should require only that exe file.

Looking at the code, it seems that ProjectInstaller is an installer component (partial class must have other source file). In that case, InstallUtil /u assembly uninstall and InstallUtil assembly installs.

You can tell whether that's how it was installed by checking for presence of Installlog files in the existing services's folder.

http://msdn.microsoft.com/en-us/library/50614e95%28v=vs.80%29.aspx

In the event, InstallUtil is not available on production machine (sometimes thats the case), then the following snippet might be useful. To install just run the exe like

assembly.exe -install
assembly.exe -uninstall

using System;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Collections;
using System.ComponentModel.Composition.Hosting;

namespace Management.WinService
{
    partial class Service : ServiceBase
    {
        private static bool _consoleMode = false;

        public static bool IsConsoleMode
        {
            get { return _consoleMode; }
        }

        /// <summary>
        /// The Main Thread: This is where your Service is Run.
        /// </summary>
        static int Main(string[] args)
        {
            if (ParseOptions(args) < 0)
                return -1;

            if (IsConsoleMode)
                return RunConsoleMode(args);

            ServiceBase[] services = { new Service() };
            ServiceBase.Run(services);
            return 0;
        }

        private static int ParseOptions(string[] args)
        {
            bool install = false, uninstall = false;
            foreach (string arg in args)
            {
                switch (arg)
                {
                    case "-i":
                    case "-install":
                        install = true; break;
                    case "-u":
                    case "-uninstall":
                        uninstall = true; break;
                    case "-c":
                    case "-console":
                        _consoleMode = true; break;
                    default:
                        Console.Error.WriteLine("Argument not expected: " + arg);
                        break;
                }
            }


            if (uninstall)
            {
                Install(true, args);
                return -1;
            }

            if (install)
            {
                Install(false, args);
                return -1;
            }

            return 0;
        }

        private static int RunConsoleMode(string[] args)
        {
            using (var service = new Service())
            {
                service.OnStart(args);
                Console.WriteLine("System running; press any key to stop");
                Console.ReadKey(true);
                service.OnStop();
                Console.WriteLine("System down; press any key to exit");
                Console.ReadKey(true);
            }
            return 0;
        }

        static void Install(bool undo, string[] args)
        {
            try
            {
                using (AssemblyInstaller inst = new AssemblyInstaller(typeof(Service).Assembly, args))
                {
                    IDictionary state = new Hashtable();
                    inst.UseNewContext = true;
                    try
                    {
                        if (undo)
                        {
                            inst.Uninstall(state);
                        }
                        else
                        {
                            inst.Install(state);
                            inst.Commit(state);
                        }
                    }
                    catch
                    {
                        try
                        {
                            inst.Rollback(state);
                        }
                        catch { }
                        throw;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Error: " + ex.Message);
            }
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:indikad
ID: 37769223
Thank you very much. Much appreciated.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now