Home > OS >  Convert C# HostBuilder pipeline to F# in isolated process Azure function
Convert C# HostBuilder pipeline to F# in isolated process Azure function

Time:02-01

The following code is from https://docs.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide

using Microsoft.Extensions.DependencyInjection;

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(s =>
    {
        s.AddSingleton<IHttpResponderService, DefaultHttpResponderService>();
    })
    .Build();
  
await host.RunAsync();

What is the F# equivalent?

CodePudding user response:

As far as translating C# to F#, the code would look something like this:

open Microsoft.Extensions.Hosting
open Microsoft.Extensions.DependencyInjection
open Microsoft.Azure.Functions.Worker.Configuration

let host =
    HostBuilder()
        .ConfigureFunctionsWorkerDefaults()
        .ConfigureServices(fun s ->
            s.AddSingleton<IHttpResponderService, DefaultHttpResponderService>()
        )
        .Build()

task {
    do! host.RunAsync()
} |> ignore

I'm unfamiliar with Azure Functions, so I don't know if RunAsync() should be something that's just ignore or something else. The docs don't make it clear, so I chose to ignore it.

  •  Tags:  
  • Related