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!