Edit on GitHub

Create your first WebService

This is a quick walkthrough of getting your first web service up and running whilst having a look at the how some of the different components work.

Step 1: Install the x dotnet tool

First we want to install the x dotnet tool:

$ dotnet tool install --global x 

The dotnet tools are ServiceStack’s versatile companion giving you quick access to a lot of its high-level features including generating mobile, web & desktop DTOs with Add ServiceStack Reference generating gRPC Clients and proto messages, quickly apply gists to your project enabled by ServiceStack’s effortless no-touch Modular features, it even includes a lisp REPL should you need to explore your remote .NET Core App in real-time.

Step 2: Selecting a template

Importantly, the dotnet tools lets you create .NET Core, .NET Framework and ASP.NET Core on .NET Framework projects. Unless you’re restricted to working with .NET Framework you’ll want to start with a .NET Core project template, for this example we’ll start with the Empty web template:

$ x new web WebApp

Step 3: Run your project

Press Ctrl+F5 to run your project!

Watched builds

An alternative to running your project in your IDE is to run a watched build using the dotnet tool on the command-line:

$ dotnet watch run

Where it will automatically rebuild & restart your App when it detects any changes to your App’s source files.

How does it work?

Now that your new project is running, let’s have a look at what we have. The template comes with a single web service route which comes from the request DTO (Data Transfer Object) which is located in the Hello.cs file:

public class Hello : IReturn<HelloResponse>
    public string Name { get; set; }

public class HelloResponse
    public string Result { get; set; }

The Route attribute is specifying what path /hello/{Name} where {Name} binds its value to the public string property of Name.

Let’s access the route to see what comes back. Go to the following URL in your address bar, where is your server address.


You will see a snapshot of the Result in a HTML response format. To change the return format to Json, simply add ?format=json to the end of the URL. You’ll learn more about formats, endpoints (URLs, etc) when you continue reading the documentation.

If we go back to the solution and find the WebApplication1.ServiceInterface and open the MyServices.cs file, we can have a look at the code that is responding to the browser, giving us the Result back.

public class MyServices : Service
    public object Any(Hello request)
        return new HelloResponse { Result = $"Hello, {request.Name}!" };

If we look at the code above, there are a few things to note. The name of the method Any means the server will run this method for any of the valid HTTP Verbs. Service methods are where you control what returns from your service.

Step 4: Exploring the ServiceStack Solution

The Recommended structure below is built into all ServiceStackVS VS.NET Templates where creating any new ServiceStack project will create a solution with a minimum of 4 projects below ensuring ServiceStack solutions starts off from an optimal logical project layout, laying the foundation for growing into a more maintainable, cohesive and reusable code-base:

Host Project

The Host project contains your AppHost which references and registers all your App’s concrete dependencies in its IOC and is the central location where all App configuration and global behavior is maintained. It also references all Web Assets like Razor Views, JS, CSS, Images, Fonts, etc. that’s needed to be deployed with the App. The AppHost is the top-level project which references all dependencies used by your App whose role is akin to an orchestrator and conduit where it decides what functionality is made available and which concrete implementations are used. By design it references all other (non-test) projects whilst nothing references it and as a goal should be kept free of any App or Business logic.

ServiceInterface Project

The ServiceInterface project is the implementation project where all Business Logic and Services live which typically references every other project except the Host projects. Small and Medium projects can maintain all their implementation here where logic can be grouped under feature folders. Large solutions can split this project into more manageable cohesive and modular projects which we also recommend encapsulates any dependencies they might use.

ServiceModel Project

The ServiceModel Project contains all your Application’s DTOs which is what defines your Services contract, keeping them isolated from any Server implementation is how your Service is able to encapsulate its capabilities and make them available behind a remote facade. There should be only one ServiceModel project per solution which contains all your DTOs and should be implementation, dependency and logic-free which should only reference the impl/dep-free ServiceStack.Interfaces.dll contract assembly to ensure Service contracts are decoupled from its implementation, enforces interoperability ensuring that your Services don’t mandate specific client implementations and will ensure this is the only project clients need to be able to call any of your Services by either referencing the ServiceModel.dll directly or downloading the DTOs from a remote ServiceStack instance using Add ServiceStack Reference:

Test Project

The Unit Test project contains all your Unit and Integration tests. It’s also a Host project that typically references all other non-Host projects in the solution and contains a combination of concrete and mock dependencies depending on what’s being tested. See the Testing Docs for more information on testing ServiceStack projects.

ServiceStack Integration


ServiceStack’s clean Web Services design makes it simple and intuitive to be able to call ServiceStack Services from any kind of client from a simple Bootstrap Website using jQuery:

        <input class="form-control" id="Name" type="text" placeholder="Type your name">
        <p id="result"></p>
    $('#Name').keyup(function () {
        var name = $(this).val();
        if (name) {
            $.getJSON('/hello/' + name)
                .success(function (response) {
        } else {

dep-free JsonServiceClient in HTML Pages

A dep-free alternative to jQuery that works in all modern browsers is to use the UMD @servicestack/client built into ServiceStack.dll along with the transpiled TypeScript Generated DTOs to enable a more typed Promise API:

<input type="text" id="txtName" onkeyup="callHello(this.value)">
<div id="result"></div>

<script src="/js/servicestack-client.js"></script>
    Object.assign(window, window['@servicestack/client']); //import into global namespace

    // generate typed dtos with https://docs.servicestack.net/typescript-add-servicestack-reference
    var Hello = /** @class */ (function () {
        function Hello(init) { Object.assign(this, init); }
        Hello.prototype.createResponse = function () { return new HelloResponse(); };
        Hello.prototype.getTypeName = function () { return 'Hello'; };
        return Hello;
    var HelloResponse = /** @class */ (function () {
        function HelloResponse(init) { Object.assign(this, init); }
        return HelloResponse;

    var client = new JsonServiceClient();
    function callHello(val) {
        client.get(new Hello({ name: val }))
            .then(function(r) {
                document.getElementById('result').innerHTML = r.result;

Used in init quick mix projects

As this requires no external deps or prescribed JS frameworks, it’s used in the init and init-lts mix scripts which you can quickly add & run in any directory with:

$ md test && cd test
$ x mix init
$ dotnet run

TypeScript or JavaScript SPA Apps

The same TypeScript JsonServiceClient is also used in more sophisticated JavaScript Apps like React Native to node.js Server Apps as well as all TypeScript SPA Project Templates, such as this example using React:

import './hello.css';

import * as React from 'react';
import { Input } from '@servicestack/react';
import { client } from '../../shared';
import { Hello } from '../../shared/dtos';

export interface HelloApiProps {
    name: string;

export const HelloApi: React.FC<any> = (props:HelloApiProps) => {
    const [name, setName] = React.useState(props.name);
    const [result, setResult] = React.useState('');

    React.useEffect(() => {
        (async () => {
            setResult(!name ? '' : (await client.get(new Hello({ name }) )).result)
    }, [name]); // fires when name changes

    return (<div>
        <div className="form-group">
            <Input value={name} onChange={setName} placeholder="Your name" />
            <h3 className="result pt-2">{ result }</h3>

Compare and contrast with other major SPA JavaScript Frameworks:

ServiceStack Services are also easily consumable from all major Mobile and Desktop platforms including native iPhone and iPad Apps on iOS with Swift, Mobile and Tablet Apps on Android with Java or Kotlin, OSX Desktop Applications as well as targeting the most popular .NET Mobile and Desktop platforms including Xamarin.iOS, Xamarin.Android, Windows Store, WPF and WinForms.

Explore ServiceStack Apps

A great way to learn new technology is to explore existing Apps built with it, where for ServiceStack you can find a number of simple focused Apps at:

Many Apps are well documented like World Validation which covers how to re-implement a simple Contacts App UI in 10 popular Web Development approaches - all calling the same ServiceStack Services.

ServiceStack is a single code-base implementation that supports .NET’s most popular Server platforms with near perfect source-code compatibility with .NET Core so all .NET Frameworks Apps are still relevant in .NET Core, e.g. the EmailContacts guidance walks through the recommended setup and physical layout structure of typical medium-sized ServiceStack projects, including complete documentation of how to create the solution from scratch, whilst explaining all the ServiceStack features it makes use of along the way.

Sharp Apps

Sharp Apps are a revolutionary new approach to dramatically simplify .NET Web App development with the ultimate simplicity of being able to develop Apps in real-time while it’s running & never needing to build or re-compile Apps. Their inherent simplicity makes it possible to build Gist Desktop Apps they can be run on-the-fly without installation and can be deployed without CI or published assets and updated with a single command.

Community Resources