Created
December 11, 2023 00:11
-
-
Save jkone27/72a0a245588d72b9d7249a4ab63015f3 to your computer and use it in GitHub Desktop.
dependency injection in fsharp using servicecollection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "nuget: Microsoft.Extensions.DependencyInjection" | |
#r "nuget: Microsoft.Extensions.Logging.Console" | |
open Microsoft.Extensions.DependencyInjection | |
open Microsoft.Extensions.Logging.Console | |
open Microsoft.Extensions.Logging | |
type ITest = | |
abstract member Log : unit -> unit | |
type Test(logger: ILogger<Test>) = | |
interface ITest with | |
member this.Log() = logger.LogInformation("cool!") | |
let services = new ServiceCollection() | |
services.AddLogging(fun l -> l.AddConsole(); ()) | |
services.AddTransient<ITest,Test>() | |
let sp = services.BuildServiceProvider() | |
//ready to roll, get your service | |
let x = sp.GetRequiredService<ITest>() | |
// invoke and see your logs, | |
// can run multiple times from repl, select this line, then: ALT + ENTER | |
x.Log() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment