SignalR + Servciestack with F# hosted on Azure
Learn how to integrate SignalR with ServiceStack using F# and deploy to Azure. Complete guide with code examples and solutions.
This may be the nastiest thing I have done with deployment in a long time. There is no problem with making it work. But deployment on Azure is a serious issue. And luckily I completed that. Yes, there is luck involved.
If you read my previous entry for running Nancy and SignalR together, then most of the things are the same.
Use Servicestack template to create a project. Remove global.asax & global.asax.fs (yes remove it!). Install SignalR asp.net package from nuget. And copy paste the code below in AppCode.fs
namespace ServicestackSignalRFSharp.App_Start
open ServiceStack
open ServiceStack.Common
open ServicestackSignalRFSharp
open ServiceStack.Razor
open System
open Owin
type AppHost() =
inherit AppHostBase("Hello F# Service",
typeof<HelloService>.Assembly)
override this.Configure container =
Diagnostics.Trace.TraceError("In servicestack configure");
this.Plugins.Add(new RazorFormat())
ignore()
static member start() =
let apphost = new AppHost()
Diagnostics.Trace.TraceError("In servicestack start");
apphost.Init() |> ignore
type Startup()=
member x.Configuration(app : Owin.IAppBuilder) =
AppHost.start()
Diagnostics.Trace.TraceError("In signalr startup");
app.MapSignalR() |> ignore
[<Microsoft.Owin.OwinStartup(typeof<Startup>)>]
do ()
Don't forget to rename the namespace as per your project namespace.
As per my Nancy article, change the index.cshtml to get SignalR client running. Once done, run it. It should work with the hello world of Servicestack and Hello Chat from SignalR.
If things still don't work, here is my github repo. Have a look at the code. And also it is deployed on Azure.
My Personal Experience
One thing I'd like to clear upfront. Even though I was able to run Servicestack and SignalR together, they are still running differently under the hood. Servicestack is hitting asp.net directly and SignalR is hitting via OWIN.
I am also not making the web service real time. It is just that both can easily run together. Making them run together was an easy task. But deploying on Azure is a serious pain in the ass.
Yes, Azure is. It is so damn easy to deploy a traditional imperative C# project as an Azure web site, but a functional F# project? It is like giving death by a thousand cuts. You solve one issue and another one comes. Just like the pipeline operator.
It is more like whatever F# is compensating for by making development easy, deployment is making it even.
That is the same with the code above. global.asax.fs is working locally. I can even put a breakpoint, but it is not working on Azure. I put a trace in every possible function, and at last found out... (Special thanks to Demis Bellot for pointing out this possible issue.)
Even though F# is reaching new heights, and I personally contributed and will continue to contribute to templates, if someone wants to take full advantage of F# in web development today with the cloud, then s/he needs to make one C# project and point F# to it. Just to get the best of both worlds.
Yes, C# is still far better when it comes to the csproj file. o_O
As usual, working with a functional language is always a fun experience, and now we have a web service that we can scale with real-time capability.
So, now.
Servicestack |> Razor |> SignalR |> Azure (with little hiccups) |> F# |> lots of love.
Frequently Asked Questions
Yes, SignalR and ServiceStack can run together in the same F# project, though they operate independently under the hood. ServiceStack hits ASP.NET directly while SignalR operates through OWIN, but both can coexist and function without conflicts.
Start by using the ServiceStack template, remove the Global.asax files, install the SignalR ASP.NET NuGet package, and then create an AppHost configuration class with a Startup class that maps SignalR in the OWIN pipeline. The configuration code should be placed in your App_Start folder with proper namespace declarations.
Azure deployment for F# functional projects presents multiple configuration and compatibility challenges that don't exist with traditional imperative C# projects. As the author notes, solving one deployment issue often reveals another, making the process significantly more complex and time-consuming.
No, they operate through different pipelines. ServiceStack processes requests directly through ASP.NET, while SignalR operates through the OWIN middleware pipeline, which is why they need separate configuration in the Startup class.
The author provides a complete GitHub repository demonstrating a working implementation, which is also deployed live on Azure at servicestacksignalrfsharp.azurewebsites.net, making it a practical reference for setup and deployment.