?
Solved

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

Posted on 2012-03-25
2
Medium Priority
?
267 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 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 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