Running .NET on Cloudflare Workers
Cloudflare Workers is a great platform for hosting. It's cheap (there is a generous free tier of 100,000 requests per day), you don't need to worry about scaling because Workers scale automatically across the edge, and you can build and run multiple versions of your app side by side. The only downside? It does not run .NET!
I've been using Cloudflare Workers (and previously Pages) for hosting my C# frontends written with Blazor WASM. That can be compiled to static files which you can deploy to Cloudflare easily. But most of the time I need some backend too. So what I did was deploy the frontend to Cloudflare, so it's served super fast. Then I only deploy the backend APIs to Azure and let the Blazor WASM hosted on Cloudflare call the APIs on Azure App Services.
Recently two new projects popped up to support running .NET on Cloudflare Workers and they both have a different approach.
JUST SHOW ME THE DEMO WEBSITES
Compile to native JavaScript
The excellent Workers project on GitHub https://github.com/iruizsalinas/workers makes it easy to write your backend in C#, make use of Cloudflare services like D1, Cache and KV from C#, and then compile it to JavaScript. You add the Workers NuGet package and call dotnet publish, and you get a native ES module out of it:
using Workers;
public static class Worker
{
[Fetch]
public static Response Fetch(Request request)
{
return new Response("Hello from C#!");
}
}
The resulting JavaScript is small in size and JavaScript runs natively on Cloudflare Workers and is super fast.
The author measured this against the older WASM approach from the same project (version 0.2.0 vs 0.3.0) and the difference is big: a cold request went from 356.8 ms to 12.6 ms, memory usage from 77.4 MB to 306 KB, and the bundle from 4.58 MB to 6.96 KB. So this is around 28x faster and 659x smaller.
One thing to know: it does not ship the full .NET runtime or the full BCL. It uses a focused C# profile, so not every NuGet package will work. Still, for a Worker that talks to KV, D1 or R2, it covers what you need.
Check the demo site I created with C# compiled to JavaScript running on Cloudflare Workers: https://cloudflare-workers-dotnet.mailpost.workers.dev/
Source code: https://github.com/michielpost/cloudflare-workers-dotnet
Compile to WASM
Another cool project is cloudflare-workers-dotnet https://github.com/nenoNaninu/cloudflare-workers-dotnet
This takes another approach and compiles it to WASM which can also run on Cloudflare Workers. It gives you a Minimal API style, so the code looks familiar:
using Cloudflare.Workers.Hosting;
var builder = WorkerApplication.CreateBuilder();
builder.MapGet("/", static _ =>
Task.FromResult(HttpResponse.Text("Hello from C# on Cloudflare Workers!")));
builder.Build().Run();
WASM is a bit heavier, the files are bigger and there is a longer startup time. For a small demo site I did not notice any problems, but you can see in the metrics it uses more resources than the JavaScript variant. The bundle from the comparison above is 4.58 MB and it needs 77.4 MB of memory, which is still fine under the 64 MiB script size and 128 MB memory limits, but it is a lot more than a few kilobytes of JavaScript.
The setup is also more involved. The .NET SDK does not support NativeAOT to WASM out of the box, so you need the experimental Microsoft.DotNet.ILCompiler.LLVM package from the dotnet-experimental feed and the WASI SDK installed and configured locally. The runtime is also single-threaded, so Task.Run and Thread.Sleep are not available and you have to use the provided helpers like WorkerTimer.Delay instead. Standard I/O is not available either.
View the demo site I created with C# as WASM running on Cloudflare Workers: https://cloudflare-workers-wasm.mailpost.workers.dev/
Source code: https://github.com/michielpost/cloudflare-workers-wasm
Local Development Setup
Because the projects get compiled to JavaScript or WASM, this makes local development a bit harder. In my sample projects I created an Aspire project to blend everything together. Compiling to JavaScript is pretty fast, but WASM takes more time, so it takes a while before you can properly test your site locally. This can negatively impact your development flow.
Which one to use?
Both projects are currently under active development and both are MIT licensed. They have a different approach.
My advice is to check them both out and see which fits best for your next C# project on Cloudflare Workers. If you want speed and a small bundle, look at the JavaScript one. If you need closer to the full .NET runtime and can live with the heavier setup, look at the WASM one. Just keep in mind that both projects are still in their development stage, things can change and things will break.