Solved

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

Posted on 2012-03-25
2
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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:…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

739 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