Book Image

ServiceStack 4 Cookbook

Book Image

ServiceStack 4 Cookbook

Overview of this book

Table of Contents (18 chapters)
ServiceStack 4 Cookbook
About the Authors
About the Reviewers


In recent times, web service APIs have become one of the most important parts of developing web applications. ServiceStack offers .NET developers a powerful set of tools that are thoughtfully architected with a core focus on simplicity, developer experience, and performance. With tools ranging from database access and HTML generation, to logging, serialization, caching, and more, ServiceStack gives developers a fantastic experience in developing web applications.

In this book, we address many of ServiceStack's features, including an introduction to using them and solving problems faced by .NET developers building web applications.

What this book covers

Chapter 1, Configuration and Routing, covers the various configuration options and ways to set up routing using the ServiceStack framework. We will also learn how to set up your solution and projects in a way that promotes testability and reuse and how to get the most out of ServiceStack's built-in IoC functionality.

Chapter 2, Services and Data Transfer Objects, says that these concepts (services and data transfer objects) are at the core of building web services with ServiceStack. This chapter teaches you techniques to produce clean, testable web services and gives examples of some common usages. You will also be shown how to use request/response filters for advanced request processing and integration.

Chapter 3, Testing and Logging, shows the integration of logging via ServiceStack LogManager, integration with popular testing frameworks as well as patterns for continuous integration.

Chapter 4, Object Relational Mapping (OrmLite), covers some of the functionality OrmLite provides while using practical patterns to keep your code clean and maintainable. OrmLite is a lightweight object relational mapping framework that focuses on simplicity and performance.

Chapter 5, HTML and Form Data, shows the different tools that the ServiceStack framework gives developers to handle HTML generation and specific interactions on web pages.

Chapter 6, Filters and Validators, shows the versatility of ServiceStack filters for different uses, including integration, as well as validators to assist with HTML forms or API error messages.

Chapter 7, Security and Authentication, shows ServiceStack OAuth and other built-in security functionality as well as how to deal with custom authentication and integration with commonly used frameworks/systems.

Chapter 8, Working with Redis, teaches you how to get the most out of the Redis client provided and ServiceStack integration as well as setting up Redis and using it in different environments.

Chapter 9, Integrating with Other Technologies, covers integration with popular and common frameworks and technologies to highlight ServiceStack's power when used in conjunction with existing systems. The chapter also covers how to handle introducing ServiceStack into an established ASP.NET MVC and WebForms project.

Appendix A, Getting Started, takes you through creating a ServiceStack solution, walking you through the different parts. This will be useful for readers who don't have access to use the ServiceStack Visual Studio extension, ServiceStackVS.

Appendix B, Testing Locally, is a short guide on how to use Fiddler2 to route traffic of a different domain to IIS Express. Depending on your environment restrictions, this might help debug OAuth providers. This isn't needed to complete the recipes in this book.

What you need for this book

While most examples simply require Visual Studio 2013 Community Edition or better, some recipes do use other technologies to illustrate integration with specific functionality within the ServiceStack framework. The following is a list of software required to use all the examples:

  • Visual Studio 2013 Community Edition or better

  • MS SQL Server 2008 R2 Express or later

  • Redis 2.8

  • MongoDB 2.6

  • IIS8 or later

Who this book is for

If you are a .NET developer who is looking for a simpler way to build services, this is the book for you. It will show you how to write fast, maintainable APIs that are a pleasure to use and maintain, starting from the database to the client and everything in between.


In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.


In this book, you will find a number of text styles 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: "ServiceStack includes ServiceStack.OrmLite, which is much faster than Microsoft's own Entity Framework and most other ORMs."

A block of code is set as follows:

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

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

  public IEnumerable<Message> Where(Expression<Func<Message, bool>> expression)
    using (var db = DbConnectionFactory.OpenDbConnection())
      return db.Select(expression);

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

c:\projects>curl http://myserver/hello/world
Hello, world!

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: " To get started, create an empty ASP.NET solution in Visual Studio. We'll name it HelloWorldService."


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 disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in 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 at

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 from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from:


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 could 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 to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted 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.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.