Solved

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

Posted on 2012-03-25
2
263 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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JQuery JavaScripot loop though RadioButtonList 4 32
bound data table problem 2 33
Tracing a live website down to the files which support it 2 25
Groupbox Control ? 2 19
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…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
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…

828 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