-
Notifications
You must be signed in to change notification settings - Fork 0
Self hosting
It's very easy to host ServiceStack in a console app or in a windows service. You just have to derive from AppHostHttpListenerBase
instead of AppHostBase
:
using ServiceStack.ServiceInterface;
using ServiceStack.WebHost.Endpoints;
class Program {
public class Hello {
public string Name { get; set; }
}
public class HelloResponse {
public string Result { get; set; }
}
public class HelloService : Service
{
public object Any(Hello request)
{
return new HelloResponse { Result = "Hello, " + request.Name };
}
}
//Define the Web Services AppHost
public class AppHost : AppHostHttpListenerBase {
public AppHost() : base("StarterTemplate HttpListener", typeof(HelloService).Assembly) { }
public override void Configure(Funq.Container container) {
Routes
.Add<Hello>("/hello")
.Add<Hello>("/hello/{Name}");
}
}
//Run it!
static void Main(string[] args)
{
var listeningOn = args.Length == 0 ? "http://*:1337/" : args[0];
var appHost = new AppHost();
appHost.Init();
appHost.Start(listeningOn);
Console.WriteLine("AppHost Created at {0}, listening on {1}", DateTime.Now, listeningOn);
Console.ReadKey();
}
}
open System
open ServiceStack.ServiceHost
open ServiceStack.WebHost.Endpoints
type Hello = { mutable Name: string; }
type HelloResponse = { mutable Result: string; }
type HelloService() =
interface IService with
member this.Any (req:Hello) = { Result = "Hello, " + req.Name }
//Define the Web Services AppHost
type AppHost =
inherit AppHostHttpListenerBase
new() = { inherit AppHostHttpListenerBase("Hello F# Services", typeof<HelloService>.Assembly) }
override this.Configure container =
base.Routes
.Add<Hello>("/hello")
.Add<Hello>("/hello/{Name}") |> ignore
//Run it!
[<EntryPoint>]
let main args =
let host = if args.Length = 0 then "http://*:1337/" else args.[0]
printfn "listening on %s ..." host
let appHost = new AppHost()
appHost.Init()
appHost.Start host
Console.ReadLine() |> ignore
0
When either of those are running you can call your hello service at http://localhost:1337/hello/World!
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 or run as a Linux Daemon which you can optionally elect to serve behind Apache or Nginx reverse proxies.
As both AppHostBase and AppHostHttpListenerBase follow the same API they are easily convertible between the two. Here's some step-by-step instructions showing you how to convert an F# Console Host to an ASP.NET Web Service on OSX!
- Why ServiceStack?
- What is a message based web service?
- Advantages of message based web services
- Why remote services should use separate DTOs
- Getting Started
- Reference
- Clients
- Formats
- View Engines 4. Razor & Markdown Razor
- Hosts
- Advanced
- Configuration options
- Access HTTP specific features in services
- Logging
- Serialization/deserialization
- Request/response filters
- Filter attributes
- Concurrency Model
- Built-in caching options
- Built-in profiling
- Messaging and Redis
- Form Hijacking Prevention
- Auto-Mapping
- HTTP Utils
- Virtual File System
- Config API
- Physical Project Structure
- Modularizing Services
- Plugins
- Tests
- Other Languages
- Use Cases
- Performance
- How To
- Future