Book Image

Mastering ServiceStack

By : Andreas Niedermair
Book Image

Mastering ServiceStack

By: Andreas Niedermair

Overview of this book

Table of Contents (13 chapters)


Over the last few decades, distributed systems have become a complete solution for the purpose of building applications on a large scale. ServiceStack is a framework for .NET developers, which offers tools ranging from the creation of APIs to accessing data in session, cache, and also the database integration of authentication and authorization, Message Queues, serialization, and much more.

In this book, we will explore the relevant features that build the foundation of a flexible, reliable, scalable, and powerful system. It also gives a deeper understanding of the configurations and patterns to solve the problems faced by a .NET developer while building distributed systems.

What this book covers

Chapter 1, Distributed Systems and How ServiceStack Jumps in, covers ServiceStack's technical basics and layout. It also introduces the design principles of APIs and the problems of distributed systems, which sets the foundation for the next chapters.

Chapter 2, ServiceStack as Your Unique Point of Access, introduces you to the IoC-container Funq and shows you how to access data from a session or cache. Finally, it teaches you how to secure your API.

Chapter 3, Asynchronous Communication between Components, introduces you to the concept of Messaging, which is then put into effect with Message Queue solutions, such as Redis and RabbitMQ. Additionally, push notifications from server to clients is covered by server-sent events (SSE).

Chapter 4, Analyzing and Tuning a Distributed System, teaches you how to add logging and profiling to ease the tracing of issues. Finally, methodologies to minimize the HTTP footprint are also introduced.

Chapter 5, Documentation and Versioning, shows you how to leverage built-in functionality to publish and modify the documentation of your API and introduces you to test clients, such as Swagger and Postman. Finally, the validation of requests is also covered.

Chapter 6, Extending ServiceStack, shows you how to write your own plugins, encapsulate services within them, and intercept requests and responses.

What you need for this book

Most examples will simply require Visual Studio 2013 Community Edition, whereas some code integrates specific softwares with ServiceStack. The following is a list of the software required to run all the examples:

  • Visual Studio 2013 Community Edition or better

  • Redis 2.8

  • RabbitMQ 3.5

Who this book is for

This book is targeted at developers who have already implemented web services with ASMX, WCF, or ServiceStack and want to gain more insight into the possibilities that ServiceStack has to offer to build distributed systems of all scales.


In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "As the NuGet package names often do not match the namespaces within, the NuGet package names are mentioned separately."

A block of code is set as follows:

public class Task
  public int Id { get; set; }
  public string Title { get; set; }
  public int UserId { get; set; }
interface IService
  Task GetTaskById(int id);
  Task[] GetAllTasks();
  Task[] GetTasksById(int[] ids);
  Task[] GetTasksForUserId(int userId);
  Task[] GetTasksByTitle(string title);
  Task[] GetTasksByTitleForUserId(string title, int userId);

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold, as shown:

private KeyValuePair<string, string>? GetCustomAuth(IRequest httpReq)
  var hasCredentials = httpReq.Dto as IHasCredentials;
  if (hasCredentials == null)
    return null;

  var userName = hasCredentials.UserName;
  var password = hasCredentials.Password;

  if (string.IsNullOrWhiteSpace(userName) ||
    return null;

  return new KeyValuePair<string, string>(userName, password);

Any command-line input or output is written as follows:

Hello John Doe!

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "One of the key points of ServiceStack is the Code-First approach."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from


Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.


You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.