Servicestack F# template. Starting from the Start

Learn how to set up ServiceStack with F# from scratch without templates. Step-by-step guide for ServiceStack V4 integration.

5 min read 848 words

I have already written a blog post about how to get started with Service Stack if anyone is using templates. It's pretty easy. Just install and run.

But as Service Stack moved to V4 with some breaking changes, I had given details about updating it. But still it is a little bit more complicated, or confusing, if I can say.

The above conversation inspired me to write this blog. And also it will be fun to write back stories.

So, if anyone wants to get started with SS without templates, they need to download the template of Nancy. Yes, the cousin sibling of SS and the other best thing that happened to the web part of .Net after .Net itself.

Create a new project and select the Empty ASP.Net option. And remove all other packages.

we are doing this just to save ourselves from using complicated project GUIDs. If anyone wanted to go raw, s/he would need to select a library project in F# and set the GUID so it will be a web application.

Now install SS from NuGet. Preferably with ASP.Net host. Use this Install-Package servicestack.Host.AspNet. If anyone prefers not to search NuGet.

Now, delete every file with .cs extension. Yes, we are doing it in F#.

Also, delete default.htm and *.js files. But keep the App_Start folder. We may need it.

It is almost done. Now, create AppHost.fs in the App_Start folder. Write a namespace you like and copy-paste the code below.

type AppHost() = 
    inherit AppHostBase("Hello F# Service", typeof<HelloService>.Assembly)
    override this.Configure container = ignore()
    static member start() = 
        let apphost = new AppHost()
        apphost.Init() |> ignore

Now, create HelloService.fs somewhere outside and copy-paste the code below.

[<CLIMutable>]
    type HelloResponse = 
        { Result : string }


    [<Route("/hello")>]
    [<Route("/hello/{name}")>]
    type Hello() = 
        interface IReturn<HelloResponse>
        member val Name = "" with get, set


    type HelloService() = 
        inherit Service()
        member this.Any(request : Hello) = { Result = "Hello " + request.Name }

Now, final settings. Add a global.asax.fs file. And copy-paste the code below.

type Global() = 
    inherit System.Web.HttpApplication()

    member x.Application_Start (sender:Object, e:EventArgs) = 
        App_Start.AppHost.start() 

We also need to add a global.asax file too.

Add this line to it:

<%@ Application Inherits="ServicestackAspNetHost.Global" %>

Do check the namespace name. This is my project's namespace.

Now, you may get an error that a couple of system references are missing. Please add them. I got errors for System.Data and also for System.Xml.Linq.

F# Visual Studio projects have poor compilation sequence. So, do keep the project file open in your favorite text editor. If there is some error like "xyxy" is not defined, most of the time it is a compilation sequence issue.

If everything builds, it is our first victory. Run it. It should show the metadata page of SS.

It is done for people who just wanted to create a back end for their single page application, mobile application, or thick client.

For the complete web story, let's add the Razor engine to it. Fire Install-Package ServiceStack.Razor in the package manager.

Add a Default.cshtml file. Add some HTML text to test.

Now, here is the complicated part. We can't add a folder in F# projects. But as it is also an ASP.Net web project, we can add an ASP.Net project folder. Add one folder and rename it to Views.

Add two files. One is _Layout.cshtml and the other is Hello.cshtml. Don't forget to set the compile type to content for all cshtml files.

Here is the content for _Layout.cshtml:

<!doctype html>
<html lang="en-us">
<head>
    <title>@ViewBag.Title</title>
    <style type="text/css">
        body {
            background: #E6E6E6;
        }
    </style>
</head>
<body>
    @RenderBody()

    <h6 style="font-size:12px; color: #999; position: fixed; bottom: -25px; right: 10px;">
        @Env.ServerUserAgent
    </h6>
</body>
</html>

And here is the content for Hello.cshtml:

@using ServiceStackAspNetHost
@inherits ViewPage<HelloResponse> 
@{
    Layout = "_Layout";
    ViewBag.Title = "Hello Page";
}

<div>
    Hello Page
    <a href="/hello">Default Hello</a>
    <a href="/hello/fsharp">Hello With get Request</a>


    <br />
    <br />
    <p>Model binding with DTO Response</p>
    <p>@Model.Result</p>

</div>

Pretty much the same as we do in C# projects.

We are almost done. Now we need to tell AppHost to add the Razor plugin. Change the code as below:

override this.Configure container =
            this.Plugins.Add(new RazorFormat())
            ignore()

Now, rebuild and run it. It will show the default page on the "/" location and the Hello page on "/Hello" location. Even you can access the metadata page too.

Not that tough, I guess. A little bit more code than normal, but in F# it will be the same as normal code in C#.

I love SS, that is true, but I love Nancy too. Will not lie on Valentine's Day. Just to clarify, as I used the Nancy template to work with SS. If I have to do some web application work, Nancy is my weapon of choice, and for API-like things, SS is always there. The people who created both are damn smart and, more importantly, friendly ones. In .Net, where the OSS and OSS community story is not that good, these people are making the difference. Obviously I can't forget to mention the F# community; they are awesome.

Frequently Asked Questions

What is the easiest way to set up ServiceStack with F# without using templates?

Start by creating an empty ASP.NET project, remove all default packages, and install ServiceStack via NuGet using `Install-Package servicestack.Host.AspNet`. Then delete all .cs files and create your F# files (AppHost.fs, your service files, and global.asax.fs) with the appropriate ServiceStack configurations.

Why do we delete all .cs files when setting up ServiceStack in F#?

Deleting .cs files removes unnecessary C# code since you're building the application entirely in F#. This keeps your project clean and focused on F# implementations only.

What files do I need to create to get a basic ServiceStack F# service running?

You need three main F# files: AppHost.fs (in the App_Start folder) to configure your application host, a service file containing your service definitions and logic (like HelloService.fs), and global.asax.fs to initialize the AppHost when the application starts.

Why use an empty ASP.NET template instead of starting from a library project?

Using the empty ASP.NET template avoids the complexity of manually setting project GUIDs to convert a library project into a web application, making the setup process simpler and less error-prone.

How do I define a service endpoint in ServiceStack with F#?

Create a type with `[<Route>]` attributes to define the endpoint path, implement the `IReturn<T>` interface to specify the response type, then create a service class inheriting from `Service` with an `Any` method that handles the request and returns the response.

Share this article