CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

WCF Hosting

288 Views
Copy Code Show/Hide Line Numbers
   1:  class Program {  
   2:      static void Main() {  
   3:          if (Environment.UserInteractive) {  
   4:              ServiceManager serviceManager = new ServiceManager();  
   5:              serviceManager.OpenAll();  
   6:              Console.ReadKey();  
   7:              serviceManager.CloseAll();  
   8:          }  
   9:          else  
  10:              ServiceBase.Run(new WindowsService());  
  11:      }  
  12:  }  
  13:    
  14:  public class WindowsService : ServiceBase  
  15:  {  
  16:      public static string WindowsServiceName = "Windows Service Name";  
  17:      public static string WindowsServiceDescription = "Windows Service Description";  
  18:      public static string WindowsServiceUsername = @".\username";  
  19:      public static string WindowsServicePassword = "password";  
  20:    
  21:      private readonly ServiceManager serviceManager = new ServiceManager();  
  22:    
  23:      private readonly IContainer components = new Container();  
  24:    
  25:      protected override void Dispose(bool disposing) {  
  26:          if (serviceManager != null) serviceManager.CloseAll();  
  27:          if (disposing && (components != null)) components.Dispose();  
  28:          base.Dispose(disposing);  
  29:      }  
  30:    
  31:      public WindowsService() {  
  32:          ServiceName = WindowsServiceName;  
  33:          CanStop = true;  
  34:      }  
  35:    
  36:      protected override void OnStart(string[] args) {  
  37:          base.OnStart(args);  
  38:          serviceManager.OpenAll();  
  39:      }  
  40:    
  41:      protected override void OnStop() {  
  42:          serviceManager.CloseAll();  
  43:          base.OnStop();  
  44:      }  
  45:  }  
  46:    
  47:  public class ServiceManager {  
  48:      readonly List<servicehost> serviceHosts = new List<servicehost>();  
  49:    
  50:      public void OpenAll() {  
  51:          OpenHost<service1>();  
  52:          OpenHost<service2>();  
  53:          ...  
  54:      }  
  55:    
  56:      public void CloseAll() {  
  57:          foreach (ServiceHost serviceHost in serviceHosts)  
  58:              serviceHost.Close();  
  59:      }  
  60:    
  61:      private void OpenHost<T>() {  
  62:          Type type = typeof(T);  
  63:          ServiceHost serviceHost = new ServiceHost(type);  
  64:          serviceHost.Open();  
  65:          serviceHosts.Add(serviceHost);  
  66:      }  
  67:  }  
  68:    
  69:  /// <remarks>  
  70:  /// Enables application to be installed as a Windows Service by running InstallUtil  
  71:  /// </remarks>  
  72:  [RunInstaller(true)]  
  73:  public class WcfServiceHostInstaller : Installer {  
  74:      public WcfServiceHostInstaller() {  
  75:          Installers.Add(new ServiceInstaller  
  76:                             {  
  77:                                 StartType = ServiceStartMode.Automatic,  
  78:                                 ServiceName = WindowsService.WindowsServiceName,  
  79:                                 Description = WindowsService.WindowsServiceDescription  
  80:                             });  
  81:          Installers.Add(new ServiceProcessInstaller { Account = ServiceAccount.User, Username = WindowsService.WindowsServiceUsername, Password = WindowsService.WindowsServicePassword });  
  82:      }  
  83:  }
by grenade
  December 21, 2009 @ 5:54am
Tags:
Description:
How to host multiple wcf services within a windows service

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate