Leverage ServiceStack to build cross platform high performance services with ease ServiceStack is a good alternative to popular Microsoft technologies like WCF and WebAPI for building scalable web services because of its simplicity, high performance, true platform independence and less configuration. This article presents an overview of ServiceStack and how we can get started using it. What is ServiceStack, anyway? ServiceStack is an open source, simple, elegant, cross platform web service framework. You can build your Web services using ServiceStack and execute them either in Windows or in a Linux environment with Mono support. Because of its message based design, ServiceStack can easily provide support for all popular formats that include: JSON, XML, JSV, SOAP and also Protobuf. A message based design promotes extensible services. ServiceStack provides asynchronous support, i.e., you can call service methods asynchronously. ServiceStack is highly testable and includes fast serializer which you can leverage for faster responses. You can use NuGet to include Protocol Buffers — a fast binary serializer from Google. ServiceStack also provides in-built support for typed validation and error handling. ServiceStack provides a rich integration with ASP.Net and ASP.Net MVC frameworks and it also includes a clean authentication and authorization provider model. ServiceStack is well documented — you can take a look at the documentation here. Now that we have had a glimpse at the basics, let’s take a quick tour on how we can get started building a simple service using ServiceStack. Getting Started To create a service using ServiceStack, follow these steps: Create a new empty ASP.Net application project Save the project with a name Next, you should add the required ServiceStack references using NuGet You should ensure that the following assemblies have been added to your project: ServiceStack ServiceStack.Client ServiceStack.Common ServiceStack.Interfaces ServiceStack.Text Once done, you should add the following lines to the web.config file of your application. <system.webServer> <validation validateIntegratedModeConfiguration="false" /> <handlers> <add path="*" name="ServiceStack.Factory" preCondition="integratedMode" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" resourceType="Unspecified" allowPathInfo="true" /> </handlers> </system.webServer> Now you can create your service that leverages ServiceStack. Creating a service using ServiceStack is way too simple. The next step is to create the request and response classes — these are standard C# classes anyway. The following code snippet illustrates the request and response classes we would be using with our service. The request class namespace IDG { public class IDGRequest { public string Address { get; set; } } } The response class namespace IDG { public class IDGResponse { public string Result { get; set; } } } The following code listing illustrates the service class. Note that your service class should extend ServiceStack.Service base class. Note how the request and response classes we created earlier have been used here. using System; using ServiceStack; namespace IDG { public class IDGService : Service { public object Post(IDGRequest request) { return new IDGResponse { Result = "Hello World" }; } } } Now that you have created the service, the next step is to host it. To do this, you should create a class that extends the ServiceStack.AppHostBase class as shown in the code snippet given next. using Funq; using ServiceStack; namespace IDG { public class IDGAppHost : AppHostBase { public IDGAppHost():base("IDG Service Host", typeof(IDGService).Assembly) { } public override void Configure(Container container) { //You can specify your service configuration details here } } } Note that ServiceStack.AppHostBase is an abstract class — you should remmeber to override the Configure() method of this abstract base class in your inherited class and specify any configuration details as needed. To bootstrap your service, you should make a call to the Init() method from the Application_Start event of the Global.asax.cs file — the following piece of code illustrates how you can achieve this. using System; namespace IDG { public class Global : System.Web.HttpApplication { protected void Application_Start() { var appHost = new IDGAppHost(); appHost.Init(); } protected void Application_End() { } protected void Session_Start(Object sender, EventArgs e) { } } } And you are done! You have created your first service using ServiceStack. You can now execute your service by pressing the F5 key and see it get started and display the supported operations in your web browser. Now that the service is up and running, you may create a service client to consume your service. To consume your service, you would need to create a project that includes the ServiceStack.Client namespace. You can include this namespace using NuGet again. Next, you can create an instance of the type of client you would like to use — this depends on the type of data your need. var xmlClient = new XmlServiceClient(“http://localhost:4416/”); var jsonClient = new JsonServiceClient(“http://localhost:4416/”); ServiceStack has been well received by the community – you can take a look at the community resources wiki page to get to know more on ServiceStack. In my future articles here, I’ll explore more on ServiceStack, the best practices that can be followed and also illustrate how it can be used in enterprise applications. Related content how-to How to use FastEndpoints in ASP.NET Core Take advantage of the free open-source FastEndpoints library to build fast and lean APIs in your ASP.NET Core applications. By Joydip Kanjilal Jul 11, 2024 7 mins Microsoft .NET C# Development Libraries and Frameworks how-to How to use Refit to consume APIs in ASP.NET Core Take advantage of Refit REST library to simplify API consumption and make your code cleaner, more efficient, and easier to maintain. By Joydip Kanjilal Jul 04, 2024 10 mins C# Microsoft .NET Software Deployment how-to When to use an abstract class vs. interface in C# Understanding the differences between an abstract class and interface is key to designing loosely coupled and extensible applications. By Joydip Kanjilal Jun 20, 2024 10 mins Small and Medium Business Microsoft .NET C# how-to 6 security best practices for ASP.NET Core Learn the best practices and built-in safeguards for preventing attacks and protecting sensitive data in your ASP.NET Core web applications. By Joydip Kanjilal Jun 07, 2024 6 mins C# Microsoft .NET Web Development Resources Videos