| title | Getting started with F# on .NET Core |
|---|---|
| description | Getting started with F# on .NET Core |
| keywords | .NET, .NET Core |
| author | cartermp |
| manager | wpickett |
| ms.date | 07/01/2016 |
| ms.topic | article |
| ms.prod | .net-core |
| ms.technology | .net-core-technologies |
| ms.devlang | dotnet |
| ms.assetid | 615db1ec-6ef3-4de2-bae6-4586affa9771 |
This article covers how you can get started with using F# on .NET Core with the ..NET Core SDK 1.0 Preview 2. It will go through building a multi-project solution with a Class Library, a Console App, and an xUnit test project.
To begin, you must install the .NET Core SDK 1.0 Preview 2.
This article uses the command line and Visual Studio Code as the text editor. You're free to use any editor you like, though. To get awesome features like Intellisense, better syntax highlighting, and more, you can also download the Ionide Extension and get a lightweight IDE experience in Visual Studio Code.
- Open up a Command Line/Terminal.
- Create a new directory named
FSNetCore. Open Visual Studio code or your preferred editor inside this directory. - Under
FSNetCore, createsrcandtestdirectories. - Under
FSNetCore, create a new file calledglobal.json. It should have this as its contents:
{
"projects":[ "src", "test" ]
}Your solutions structure should now look like this:
FSNetCore/
|---src/
|---test/
|---global.json
-
Create a
Libraryfolder underFSNetCore/src. -
In the command line, execute
dotnet new -l F#inFSNetCore/src/Library. -
Remove the
NuGet.Configfile. -
Rename
Program.fstoLib.fs. -
Open the
project.jsonfile and remove theemitEntryPointentry frombuildOptions. -
Under
buildOptions/compile/includeFiles, replaceProgram.fswithLib.fs. -
Remove the global
dependenciessection. -
Under
tools/dotnet-compile-fsc, remove theimportssection. -
Under
frameworks, changenetcoreapp1.0tonetstandard1.6. -
Under
frameworks/netstandard1.6, remove theimportssection. -
Under
frameworks/netstandard1.6/dependencies, replace theMicrosoft.NETCore.Apppackage with"NETStandard.Library":"1.6.0". Add"Microsoft.FSharp.Core.netcore": "1.0.0-alpha-160629"and"Newtonsoft.Json": "9.0.1". -
Open
Lib.fsand change the contents to the following code:module Library open Newtonsoft.Json let getJsonNetJson value = sprintf "I used to be %s but now I'm %s!" value (JsonConvert.SerializeObject(value))
-
Run
dotnet restoreanddotnet build. These should succeed.
Your project.json file should look like this:
{
"version": "1.0.0-*",
"buildOptions": {
"compilerName": "fsc",
"compile": {
"includeFiles": [
"Lib.fs"
]
}
},
"tools": {
"dotnet-compile-fsc":"1.0.0-preview2-*"
},
"frameworks": {
"netstandard1.6": {
"dependencies": {
"NETStandard.Library":"1.6.0",
"Microsoft.FSharp.Core.netcore": "1.0.0-alpha-160629",
"Newtonsoft.Json": "9.0.1"
}
}
}
}And your Lib.fs file should look like this:
module Library
open Newtonsoft.Json
let getJsonNetJson value =
sprintf "I used to be %s but now I'm %s!" value (JsonConvert.SerializeObject(value))-
Create an
Appfolder underFSNetCore/src. -
In the command line, execute
dotnet new -l F#inFSNetCore/src/App. -
Remove the
NuGet.Configfile. -
Open the
project.jsonfile. -
Remove the global
dependenciessection. -
Under
tools/dotnet-compile-fsc, remove theimportssection. -
Under
frameworks/netcoreapp1.0/, remove theimportssection. -
Under
frameworks/netcoreapp1.0/dependencies, add the following afterMicrosoft.NETCore.App:"Microsoft.FSharp.Core.netcore": "1.0.0-alpha-160629", "Library":{ "version":"1.0.0", "target": "project" }
-
Change
Program.fsto:open System open Library [<EntryPoint>] let main argv = printfn "Nice command line arguments!. Here's what JSON.NET has to say about them:" argv |> Array.map getJsonNetJson |> Array.iter (printfn "%s") 0 // return an integer exit code
-
Enter
dotnet restoreanddotnet buildinto the command line. These should succeed. -
Enter
dotnet run Hello Worldinto the command line. You should see results like this:
Nice command line arguments! Here's what JSON.NET has to say about them:
I used to be Hello but now I'm ""Hello""!
I used to be World but now I'm ""World""!
Your project.json file should look like this:
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true,
"compilerName": "fsc",
"compile": {
"includeFiles": [
"Program.fs"
]
}
},
"tools": {
"dotnet-compile-fsc":"1.0.0-preview2-*"
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"Microsoft.FSharp.Core.netcore": "1.0.0-alpha-160629",
"Library":{
"version":"1.0.0",
"target": "project"
}
}
}
}
}And your Program.fs file should look like this:
open System
open Library
[<EntryPoint>]
let main argv =
printfn "Nice command line arguments!. Here's what JSON.NET has to say about them:"
argv
|> Array.map getJsonNetJson
|> Array.iter (printfn "%s")
0 // return an integer exit code-
Create a
TestLibraryfolder underNETCoreFS/test. -
In the command line, execute
dotnet new -l F#inFSNetCore/src/Tests. -
Remove the
NuGet.Config. -
Rename
Program.fstoTests.fs. -
Open the
project.jsonfile. -
Remove the
emitEntryPointentry underbuildOptions. -
Under
buildOptions/compile/includeFiles, replaceProgram.fswithTests.fs. -
Remove the global
dependenciessection. -
Under
tools/dotnet-compile-fsc, remove theimportssection. -
Under
frameworks/netcoreapp1.0/, remove theimportssection. -
Under
frameworks/netcoreapp1.0/dependencies, add the following afterMicrosoft.NETCore.App:"Microsoft.FSharp.Core.netcore": "1.0.0-alpha-160629", "xunit":"2.2.0-beta2-build3300", "dotnet-test-xunit":"2.2.0-preview2-build1029", "Library":{ "version": "1.0.0", "target": "project" },
-
After the
frameworkssection, add"testRunner":"xunit". Note that you can add this section anywhere in theproject.jsonfile. -
In
test.fs, paste the following code:module Test open Xunit open Library [<Fact>] let ``Library converts "Banana" correctly``() = let expected = """I used to be Banana but now I'm "Banana"!""" let actual = getJsonNetJson "Banana" Assert.Equal(expected, actual)
-
Run
dotnet restoreanddotnet build.
You should now be able to run the test and verify it passes by doing dotnet test.
Note
This will temporarily fail on macOS. There is an issue here to track this.
Your project.json file should look like this:
{
"version": "1.0.0-*",
"buildOptions": {
"compilerName": "fsc",
"compile": {
"includeFiles": [
"Tests.fs"
]
}
},
"tools": {
"dotnet-compile-fsc":"1.0.0-preview2-*"
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"Microsoft.FSharp.Core.netcore": "1.0.0-alpha-160629",
"xunit":"2.2.0-beta2-build3300",
"dotnet-test-xunit":"2.2.0-preview2-build1029",
"Library":{
"version": "1.0.0",
"target": "project"
},
}
}
},
"testRunner": "xunit"
}And your Tests.fs file should look like this:
module Test
open Xunit
open Library
[<Fact>]
let ``Library converts "Banana" correctly``() =
let expected = """I used to be Banana but now I'm "Banana"!"""
let actual = getJsonNetJson "Banana"
Assert.Equal(expected, actual)