Skip to content
mythz edited this page Jul 1, 2012 · 15 revisions

It is very easy to host ServiceStack in a console app or in a windows service. You simply have to derive from AppHostHttpListenerBase instead of AppHostBase:

	class Program {
		const string ListeningOn = "http://localhost:1337";

		public class AppHost : AppHostHttpListenerBase {
			public AppHost() : base("StarterTemplate HttpListener", typeof(HelloService).Assembly) { }

			public override void Configure(Funq.Container container)
			{
				container.Register(new TodoRepository());
				Routes.Add<Hello>("/hello/{Name}");
			}
		}

		static void Main(string[] args)
		{
			var appHost = new AppHost();
			appHost.Init();
			appHost.Start(ListeningOn);

			Console.WriteLine("AppHost Created at {0}, listening on {1}", DateTime.Now, ListeningOn);
			Console.ReadKey();
		}

		public class Hello {
			public string Name { get; set; }
		}

		public class HelloResponse {
			public string Result { get; set; }
		}

		public class HelloService : ServiceBase<Hello> 
		{
			protected override object Run(Hello request) 
			{
				return new HelloResponse { Result = "Hello, " + request.Name };
			}
		}
	}


This will run in as a Console Host in any Operating System with .NET 3.5 or Mono installed. In addition this can also be wrapped-up and run inside a [Windows Service](https://github.com/ServiceStack/ServiceStack.Examples/tree/master/src/StarterTemplates/WinServiceAppHost) or run as a [Linux Daemon](https://github.com/ServiceStack/ServiceStack/wiki/Run-ServiceStack-as-a-daemon-on-Linux) which you can optionally elect to serve behind [Apache or Nginx reverse proxies](https://github.com/ServiceStack/ServiceStack/wiki/Run-ServiceStack-as-a-daemon-on-Linux).

Clone this wiki locally