Getting started with ServiceStack – Creating a service

ServiceStack is a powerful platform that allows developers to do lots of different thinks. It is an alternative to popular Microsoft technologies WCF, ASP.NET MVC and WebAPI. It promotes true platform independence, high performance, lesser configuration mess (oh yes!) and above all, simplicity. Let’s see how to get started with creating services using ServiceStack.
In this simple example I will be trying to replicate a Flight Service in the Travel industry. I am going to use an ASP.NET web application as the host of this service for simplicity. So, the first step is to add a new empty ASP.NET web application in a VisualStudio solution.


Next, we need to add required ServiceStack references into the project. I am using NuGet package manager to get this done.


Make sure that you have following references added into the project.


Next, change the content of Web.config of the web project as shown below in order to enable ServiceStack.


Now, we can go ahead and create the service. Creating a service using ServiceStack is super easy! As discussed earlier, let’s add a simple service to imitate the behaviour of a flight service. This service receives a request that specifies Arrival and Departure points and returns a response with the lowest fare. First, let’s add classes for request


As you can see, the request class is very simple. as it only specify departure and arrival ports. Now, lets define the response.


The response contains the lowest fare and other important data such as selected airline and times of departure and arrival at the destination.

Now we can create the service class. Creating a service in ServiceStack is very simple. All we need to do is to make sure that the service is inherited from ServiceStack.Service class. I have defined just one simple operation in the service called Post() that accepts a FlightRequest object and returns a FlightResponse object. We are not going to do anything intelligent inside the service operation in this demonstration. I have assigned some arbitrary values to the response object to mimic some business functionality.


We are done with the service now. See how easy it is! In order to host the newly designed service, we need to create an application host class. Let’s call it FlightAppHost.


This class inherits from ServiceStack.AppHostBase abstract class. We must override the Configure() method of this base class. Inside the Configure() method we have the option to configure the service. We’re going to skip that step for this example. Also, note that we are using an IoC container called Func which is one of the fastest IoC containers available at the present.

To get the service host bootstrapped, we need to start the application host that we’ve just developed. Let’s add the following piece of code into the Application_Start event in the Global.asax file so, that the service host is launched when the web application is started.


Now we are ready to fire up our service and test whether it is working. Just press F5 to start debugging the web application and you could see that the service starts in your default browser.


You can see that the request type used in the operation of the FlightService has been identified as the operation of the service (of course, we may add more operations if we wish to). You may also notice that different types of sample requests and responses (XML, JSON, JSV, etc.) that you may use with this operation can also be viewed by clicking on the corresponding link.
That’s it! You have just created a service using ServiceStack!


About Dilan Perera

I am a Microsoft .NET evangelist (C#) with more than 8 years of hands on experience in software engineering. I have a Bachelor of Engineering degree and a number of certifications including MCPD, MCTS, MCDBA, MCSD, MCAD, and MCP.
This entry was posted in ServiceStack and tagged . Bookmark the permalink.

4 Responses to Getting started with ServiceStack – Creating a service

  1. Pingback: ServiceStack – Testing services with Chrome REST Console | Dilan's Blogs

  2. Pingback: ServiceStack – Use routes to customise service endpoints | Dilan's Blogs

  3. Pingback: ServiceStack – more on Routes | Dilan's Blogs

  4. Pingback: ServiceStack – Writing C# clients for services | Dilan's Blogs

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s