title: Controlling Windows Service (Start, Stop and more..) with self hosted NancyFX web interface
body: <p>My goal is to create a simple web interface for a windows service that will allow me to interact with the service using a web browser. </p>
  
  <p>By interaction I mean that by pressing a button in an html page I will be able to call <code>Stop()</code> method defined inside the service class. (or any other methods available)</p>
  
  <p>I have a class that is going to be hosted inside a ServiceHost, running as a Windows Service.
  The hierarchy is the following:</p>
  
  <pre><code>public interface IMyServiceInterface
  {
          string Name { get; set; }
          void Stop();
  }
  
  public class MyServiceBase : IMyServiceInterface
  {
    string sName;
    public string Name
    {
      get {return sName; }
      set {sName = value;}
    }
  
    public virtual void Stop() {}
  }
  
  public class dbSynchronizer : MyServiceBase 
  {
     public dbSynchronizer()
     {
        this.Name = "dbSynchronizer_01";
     }
  
     public override void Stop()
     {
        base.Stop();
      }
  }
  </code></pre>
  
  <p>The code for starting the hosts:</p>
  
  <p>File: <strong>Program.cs</strong></p>
  
  <pre><code>static void Main(string[] args)
  {
     ServiceBase[] ServicesToRun; 
     ServicesToRun = new ServiceBase[]
     {
       new dbSynchronizerService()
     };
     ServiceBase.Run(ServicesToRun);
  }
  </code></pre>
  
  <p>File: <strong>dbSynchronizerService.cs</strong></p>
  
  <pre><code>public partial class dbSynchronizerService : ServiceBase
  {    
     NancyHost nancyHost;
     dbSynchronizer  dbSynchronizerService;
  
     protected override void OnStart(string[] args)
     {
  
        dbSynchronizerInstance = new dbSyncronizerLib.dbSynchronizer();
  
        CustomBootstrapper _custBootStrapper = new CustomBootstrapper();
        nancyHost = new Nancy.Hosting.Self.NancyHost(new                 Uri("http://localhost:9664"), _custBootStrapper);                
  
        if (nancyHost != null)
            nancyHost.Start();
  
        sHost = new ServiceHost(dbSynchronizerInstance);
        sHost.Open();           
       }    
  }
  </code></pre>
  
  <p>Inside the MainModule defined for Nancy I need to get access to the current isntance of dbSynchronizer class (currently being executed inside the service host) to gain access to methods and properties.
  To begin with, I just want a simple view that will display the name of the service like below:</p>
  
  <p>File: <strong>MainModule.cs</strong>:</p>
  
  <pre><code>public class MainModule : NancyModule
      {
          public IMyServiceInterface _myService;
  
          public MainModule(IMyServiceInterface _service)
          {
              _myService= _service;
  
              Get["/"] = x =&gt;
              {
  
                  var model = new ConfigStatusModel
                  {
                      Message = String.IsNullOrEmpty(_service.Name) ? "Cannot get service name" : _service.Name
                  };
  
                  return View["index.html", model];
              };
          }
      }
  </code></pre>
  
  <p>After this step, I would also like to make some POST to some other modules to invoke methods such as <code>Stop()</code> or any other methods that are supposed to be triggered by human intervention: <code>DoSyncDb(), CleanTable().</code></p>
  
  <p>At this point, when running the service and requesting the <code>http://localhost:9664/</code> I get the "Cannot get service name" value in the model attached to the view.</p>
  
  <p>Can some one please tell me what I am doing wrong or point me to a working sample? Or maybe tell me how can what I described above be achieved properly.</p>
  
  <p>Thank you.</p>