Hi there wondering is there any facilities in Giraffe to pass a function inn configureServices instead of type which corresponds to create a class inheriting from IHostedService / BackgroundService?
Hi, currently there is no such a facility.
I think we would have to come up with a function signature which would be generic enough to meet more than just one person's needs. Giraffe would need to implement a class deriving from BackgroundService and then implement some boilerplate code to accept a function and run it on loop with a TimeSpan configured somewhere which gets passed into a Task.Delay.
Then we'd need an extension method to configure that function with a timespan and register the BackgroundService on behalf of the user. That function would also require to have a services parameter so that other registered services are still accessible. It's not complicated, but not sure if this would add much value beyong removing a bit of boilerplate code.
The real question is though, does such a function really belong to Giraffe? Since .NET Core 2.1 I think most people will use BackgroundServices for non HTTP related jobs and at this point I wonder if Giraffe adds much value since Giraffe is all about HTTP?
Would love to hear a few use cases how people would like to use this and what else they'd use from Giraffe in those cases then...
What I had in my mind was a function like that would represent this method:
This function could be then passed to whatever function to the giraffe that would do all the boiler for the developer.
My point is that sure every feature implementation takes some efforts, but if the purpose of giraffe is to provide "A functional ASP.NET Core micro web framework for building rich web applications." then it would be good that to the extent possible things are offered as functions and does not require to implement classes.
Of course you can still use OO-like coding with F# (otherwise they would not be much interop with the existing .NET ecosystem) and tbh this is what we are doing at my company when using F# with ASP.NET Core.
What we noticed however is that we end doing creating a lot of classes like IHostedService implementations and BackgroundService child classes to fit our needs. But not only that.
@kerry-perret take a look at this project. I tried something somewhat simple to accomplish what you re after. With some modifications, I believe you can tailor it to your needs...
Look at line 87 (JobTrigger module)
https://github.com/AlbertoDePena/FSharpFunctions/blob/master/FSharpFunctions.Host/Runtime.fs
I'm going to close this issue because whilst I agree that it would be really nice to have a functional way of creating a hosted service and I think it would be a great idea to have such a project or package which would provide such functionality, I don't think that Giraffe is the right place for it. A generic hosted service which isn't a web application would not be able to use anything from Giraffe as all Giraffe offers is a functional router and HttpContext related stuff. It's a great idea though and would love if someone was to start such a project.
Most helpful comment
Hi, currently there is no such a facility.
I think we would have to come up with a function signature which would be generic enough to meet more than just one person's needs. Giraffe would need to implement a class deriving from
BackgroundServiceand then implement some boilerplate code to accept a function and run it on loop with aTimeSpanconfigured somewhere which gets passed into aTask.Delay.Then we'd need an extension method to configure that function with a timespan and register the BackgroundService on behalf of the user. That function would also require to have a services parameter so that other registered services are still accessible. It's not complicated, but not sure if this would add much value beyong removing a bit of boilerplate code.
The real question is though, does such a function really belong to Giraffe? Since .NET Core 2.1 I think most people will use
BackgroundServices for non HTTP related jobs and at this point I wonder if Giraffe adds much value since Giraffe is all about HTTP?Would love to hear a few use cases how people would like to use this and what else they'd use from Giraffe in those cases then...