Creating a simple service using ServiceStack


ServiceStack is a framework similar to ASP.Net MVC, Web API, WCF etc. used to build web applications and services. The framework is platform independent and the applications built using it can be run on both Windows and Linux. In this article we will build a simple REST based web service that will return ‘Note’ details associated with a given ‘Id’. When the ‘Id’ parameter is not specified in the request all the ‘Notes’ are returned to the client.


Following are the pre-requisites

  1. Visual Studio 2015
  2. ‘ServiceStack’ NuGet Package

Follow the below steps to create the base project

  1. Create a new project using the ‘Empty’ ASP.Net application project template.
  2. Add ‘Global.asax’ file to the project.
  3. Install the ‘ServiceStack’ NuGet package in this project.

Request and Response

The ServiceStack framework uses the Data Transfer Objects design pattern. This requires defining different classes for the Request and Response messages for a given Service that are independent of the domain specific classes that are used inside the Service. We will create two classes for this.

Add request and response DTO classes

Following two classes represent the input and output DTO objects of the NotesService.

Define a Route mapping for the Request DTO

The NoteRequestDTO class is used to define the URL Route used to invoke the service. Here we have defined two routes

This defines the URL for retrieving all the Notes.
This defines the URL for retrieving a specific Note.


The NotesService class defines the implementation of a service that returns Notes. This class should extend the ‘Service’ class of ServiceStack. The class defines only one method ‘Any’ which will Handle all the HTTP method types, viz., GET, POST, PUT and DELETE. This method takes the Request DTO class defined earlier as a parameter. The return type varies based on the input. The service has a predefined list of Notes. ServiceStack framework will parse the input and fills the Request DTO object. When the Id parameter is not passed we need to return the whole Notes collection. If the specific Id is passed we look up and return the corresponding Note. If there is no Note found corresponding to the Id then we just return an empty string.


The service now needs to be bootstrapped. This is done by creating a class that is derived from AppHostBase. The base class constructor needs to be initialized with the Service type that it is bootstrapping. Then the abstract method Configure needs to be implemented.

Start Listening

The web service needs to be initialized so that it can accept the requests. Inside the ASP.Net Web site this should done inside the Global.asax file. Add the ‘Global.asax’ if not already present.

Add the Host Initialization

Added the following in Application_Start() method of Global.asax file.

Register the ServiceStack with ASP.Net framework

Add the following configuration element to web.config at the same level as

Running the Web Service

Build and run from the Visual studio. The website metadata page will open showing various ways of invoking the service. The following image shows the browser screenshot.
metadata of service

Invoking the Web Service

We can invoke the service using the browser as follows

  • Get all Notes
    Retrieve all notes
  • Get a specific Note
    Retrieve single note


This article explains the procedure to build and test a simple service using the ServiceStack framework.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.