Click here to Skip to main content
15,892,298 members
Articles / Programming Languages / C#

Hosting multiple services in separate AppDomains

Rate me:
Please Sign up or sign in to vote.
4.61/5 (9 votes)
20 Jun 2007CPOL3 min read 59.4K   871   47  
An article that discusses hosting multiple services in separate AppDomains
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
using System.IO;
using ServiceHost.Shared;
using System.Xml.Serialization;
using System.Reflection;

namespace ServiceOne
{
    [RunInstaller(true)]
    public partial class Installer : System.Configuration.Install.Installer
    {
        public Installer()
        {
            InitializeComponent();
        }


        protected override void OnAfterInstall(System.Collections.IDictionary savedState)
        {
            base.OnAfterInstall(savedState);

            // create and initialize the service config info
            ServiceConfigInfo configInfo = new ServiceConfigInfo();
            configInfo.ApplicationBase = Path.GetDirectoryName(this.Context.Parameters["assemblyPath"]);
            configInfo.ConfigFileName = "ServiceOne.dll.config";
            configInfo.TypeName = typeof(ServiceOne).FullName;
            configInfo.AssemblyName = Assembly.GetExecutingAssembly().FullName.Split(new char[] { ',' })[0];

            // construct the service config file path and create directory if neccessary
            string filePath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
            filePath = Path.Combine(filePath, "ServiceHost");
            Directory.CreateDirectory(filePath);
            filePath = Path.Combine(filePath, "ServiceOne.config.xml");

            // write the service configuration for this service
            StreamWriter writer = new StreamWriter(filePath);
            using (writer)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(ServiceConfigInfo));
                serializer.Serialize(writer, configInfo);
            }

            // restart the service if it is running
            ServiceController serviceController = new ServiceController("ServiceOne");
            if (serviceController.Status == ServiceControllerStatus.Running)
            {
                serviceController.Stop();
                serviceController.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 20));
                serviceController.Start();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
I am a consultant, trainer, software archtect/engineer, since the early 1980s, working in the greater area of Boston, MA, USA.

My work comprises the entire spectrum of software, shrink-wrapped applications, IT client-server, systems and protocol related work, compilers and operating systems, and more ....

I am currently focused on platform development for distributed computing in service oriented data centers.

Comments and Discussions