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 feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos