Introduction

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.

Pre-requisites

Following are the pre-requisites:

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

Here are the 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.

NotesAppHost

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.
  1. namespace NotesServer.AppHost
  2. {
  3. public class NotesAppHost : AppHostBase
  4. {
  5. public NotesAppHost() : base("Notes Service", typeof(NotesService).Assembly)
  6. {
  7. }
  8. public override void Configure(Container container)
  9. {
  10. }
  11. }
  12. }
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.
  1. protected void Application_Start(object sender, EventArgs e)
  2. {
  3. new NotesAppHost().Init();
  4. }
Register the ServiceStack with ASP.Net framework

Add the following configuration element to web.config at the same level as <system.web>
  1. <system.webServer>
  2. <validation validateIntegratedModeConfiguration="false" />
  3. <handlers>
  4. <add path="*" name="ServiceStack.Factory" type="ServiceStack.HttpHandlerFactory, ServiceStack"
  5. verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
  6. </handlers>
  7. </system.webServer>
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.

service

Invoking the Web Service

We can invoke the service using the browser as in the following screenshot:
  1. Get all Notes.

    Get all Notes

  2. Get a specific Note.

    Get a specific Note

Conclusion

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