title: Running Topshelf service using ConfigureServiceInIsolation - Generic Host
body: <p>I am developing a very simple Generic Host solution that will allow us to host assemblies as windows services (ala NServiceBus). I'm coming across the following exception (similar to the comments mentioned on Dru's <a href="http://codebetter.com/blogs/dru.sellers/archive/2009/03/18/topshelf-isolated-services.aspx" rel="nofollow noreferrer">blog post</a>). I need this to work so I can host services in different AppDomains.</p>
  
  <p>"Type 'MyProject.WindowsServices.GenericHost.Program+&lt;>c__DisplayClass5' in Assembly 'MyProject.WindowsServices.GenericHost, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable."</p>
  
  <p>I'm using the Topshelf 1.0 RC binaries available from the download link on the topshelf homepage (topshelf-project.com). I've tried the latest build (29/07/2010), and the builds avialable for download from google code and github! I can't get any of them to work for me!</p>
  
  <p>This is working in the NServiceBus library with an older version of Topshelf (the dll is versioned 0.8.0.96). With some minor code changes to what I have below (use CreateServiceLocator in place of HowToBuildService) it works for me with these older binaries, but I'd rather stick to the latest code to take advantage of any planned fixes or enhancements.</p>
  
  <p>Here is my code.</p>
  
  <pre><code>static void Main(string[] args)
  {
      ArgumentParser arguments = new ArgumentParser(args);
      string configFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
          arguments.ServiceType.Assembly.ManifestModule.Name + ".config");
  
      RunConfiguration cfg = RunnerConfigurator.New(x =&gt;
      {
          x.SetServiceName(arguments.ServiceName);
          x.SetDisplayName(arguments.DisplayName);
          x.SetDescription(arguments.Description);
  
          if (string.IsNullOrEmpty(arguments.UserName))
          {
              x.RunAsLocalSystem();
          }
          else
          {
              x.RunAs(arguments.UserName, arguments.Password);
          }
  
          x.ConfigureServiceInIsolation&lt;GenericHost&gt;(c =&gt;
          {
              c.ConfigurationFile(configFile);
              c.Named(arguments.ServiceType.AssemblyQualifiedName);
              c.HowToBuildService(name =&gt; new GenericHost(arguments.ServiceType));
              c.WhenStarted(tc =&gt; tc.Start());
              c.WhenStopped(tc =&gt; tc.Stop());
          });
      });
  
      Runner.Host(cfg, args);
  }
  </code></pre>
  
  <p>Also of note is that my GenericHost class and the class identified by arguments.ServiceType both implement MarshalByRefObject and I have also made these classes Serializable to see if that would help. It's not these classes that are causing the problem though, it appears to be complaining about an anonymous type generated by the C# compiler for one or more of the lambda's I have configured.</p>
  
  <p>Is anyone else seeing this problem with using ConfigureServiceInIsolation()? If not, does anyone know what I'm missing here? Let me know if you need more info, e.g. stack trace or more code.</p>