?
Solved

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

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

650 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