Home

I need help converting an asp.net core webserver from C# to F# in linqpad 6

I am trying to translate this linqpad query to F#

void Main() { using (var host = WebHost .CreateDefaultBuilder() .UseStartup<Startup>() .Build()) { host.RunAsync(); Util.ReadLine(); } } [Route("api")] public class TestController : ControllerBase { [HttpGet("testGet")] public int TestGet() { return 121221; } [HttpPost("testPost")] public string TestPost() { return String.Empty; } } // Define other methods, classes and namespaces here public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddRouteAnalyzer(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime applicationLifetime, IRouteAnalyzer routeAnalyzer) { app.UseMvc(); applicationLifetime.ApplicationStarted.Register(() => routeAnalyzer.GetAllRouteInformations().Select(i => i.ToString()).Dump("======== ALL ROUTE INFORMATION ========")); } }
The query prints the following list of routes
RouteInformation{Area:"", HttpMethod: "GET", Path:"/api/testGet", Invocation:"TestController.TestGet (TestController.TestGet (LINQPadQuery))"}
RouteInformation{Area:"", HttpMethod: "POST", Path:"/api/testPost", Invocation:"TestController.TestPost (TestController.TestPost (LINQPadQuery))"}


Here is the F# version which does not show any routes

type Startup () = member this.ConfigureServices(services: IServiceCollection) = services .AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2) |> ignore services.AddRouteAnalyzer() |> ignore member this.Configure(app: IApplicationBuilder, env: IHostingEnvironment, applicationLifetime: IApplicationLifetime, routeAnalyzer:IRouteAnalyzer ) = app.UseMvc() |> ignore applicationLifetime.ApplicationStarted.Register(fun _ -> (routeAnalyzer.GetAllRouteInformations() |> Seq.map (sprintf "%O")).Dump("======== ALL ROUTE INFORMATION ========") |> ignore ) |> ignore [<Route("api")>] type TestController() = inherit ControllerBase() [<HttpGet("testGet")>] member this.TestGet() = 121221 [<HttpPost("testPost")>] member this.TestPost() = String.Empty let buildHost() = Microsoft.AspNetCore.WebHost .CreateDefaultBuilder() .UseStartup<Startup>() .Build() let startServer() = use host = buildHost() host.RunAsync() |> ignore Util.ReadLine() startServer()

Any idea why i don't get the same result for both ?
Sign In or Register to comment.