I spent some time searching for the Akka.NET F# API. Could not find it, even though there is good C# documentation. I found the code below, dated March 2017, which looks good, but unfortunately generates an exception when I try to run it.
Two questions:
1) What is wrong with the code below?
2) Is there online documentation for the Akka.Net F# API and if yes, where is it?
Observation: I tried several other F# Akka.NET snippets I found online and all of them generated exceptions.
The URL for the code is:
https://www.seventeencups.net/building-a-mud-with-f-sharp-and-akka-net-part-one/
And here is the code I tried to run:
open System
open Akka.Actor
open Akka.Configuration
open Akka.Remote
open Akka.FSharp
let system = System.create "system" (Configuration.defaultConfig())
type GreeterMsg =
| Hello of string
| Goodbye of string
let greeter = spawn system "greeter" <| fun mailbox ->
let rec loop() = actor {
let! msg = mailbox.Receive()
match msg with
| Hello name -> printf "Hello, %s!\n" name
| Goodbye name -> printf "Goodbye, %s!\n" name
return! loop()
}
loop()
The exception message includes the following:
System.TypeLoadException: Method 'WatchWith' in type '-ctor#270' from assembly 'Akka.FSharp, Version=1.2.3.41, Culture=neutral, PublicKeyToken=null' does not have an implementation
WatchWith method has been introduced in Akka.NET v1.3, while you're using Akka.FSharp v1.2.3. You'll need to downgrade your Akka dependency back to 1.2.3 (at this point in time Akka.FSharp is not yet available in v1.3).
Related
I am trying to use Akka.NET for F# in VSCode using this example I found on the internet.
Code
// ActorSayHello.fsx
#time "on"
// #load "Bootstrap.fsx"
open System
open Akka.Actor
open Akka.Configuration
open Akka.FSharp
open Akka.TestKit
// #Using Actor
// Actors are one of Akka's concurrent models.
// An Actor is a like a thread instance with a mailbox.
// It can be created with system.ActorOf: use receive to get a message, and <! to send a message.
// This example is an EchoServer which can receive messages then print them.
let system = ActorSystem.Create("FSharp")
type EchoServer =
inherit Actor
override x.OnReceive message =
match message with
| :? string as msg -> printfn "Hello %s" msg
| _ -> failwith "unknown message"
let echoServer = system.ActorOf(Props(typedefof<EchoServer>, Array.empty))
echoServer <! "F#!"
system.Shutdown()
But I am getting this error
ActorSayHello.fsx(6,6): error FS0039: The namespace or module 'Akka' is not defined.
I have configured ionide plugin in VScode and am able to run F# without Akka.NET. I installed Akka.NET using the following command
dotnet add package Akka.FSharp --version 1.4.10
The library got added automatically added to .fsproj file. This is the output of the command
Any help would be greatly appreciated. Thank you!
Since you are using a script file it doesn't contain any project reference.
Instead you can use nuget references directly from the script!
#r "nuget: Akka.FSharp"
#r "nuget: Akka.TestKit"
Another caveat that might occur is that F# interactive needs a --langversion:preview flag to be able to use nuget references. Setting "FSharp.fsiExtraParameters": ["--langversion:preview"] in the VSCode settings.json did the trick for me.
And the last bit that I had to change to make it compile was to replace system.Shutdown() with system.Terminate() because Shutdown method was deprecated and removed.
Here is the full listing with changes mentioned above:
// ActorSayHello.fsx
#time "on"
#r "nuget: Akka.FSharp"
#r "nuget: Akka.TestKit"
// #load "Bootstrap.fsx"
open System
open Akka.Actor
open Akka.Configuration
open Akka.FSharp
open Akka.TestKit
// #Using Actor
// Actors are one of Akka's concurrent models.
// An Actor is a like a thread instance with a mailbox.
// It can be created with system.ActorOf: use receive to get a message, and <! to send a message.
// This example is an EchoServer which can receive messages then print them.
let system = ActorSystem.Create("FSharp")
type EchoServer =
inherit Actor
override x.OnReceive message =
match message with
| :? string as msg -> printfn "Hello %s" msg
| _ -> failwith "unknown message"
let echoServer = system.ActorOf(Props(typedefof<EchoServer>, Array.empty))
echoServer <! "F#!"
system.Terminate()
And the result looks like this on my machine:
Real: 00:00:00.000, ЦП: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
namespace FSI_0012.Project
Hello F#!
Real: 00:00:00.007, ЦП: 00:00:00.000, GC gen0: 1, gen1: 0, gen2: 0
val system : ActorSystem = akka://FSharp
type EchoServer =
class
inherit Actor
override OnReceive : message:obj -> unit
end
val echoServer : IActorRef = [akka://FSharp/user/$a#989929929]
val it : Threading.Tasks.Task
I have installed the MathNet.Numerics package on Visual Studio 2017 using the Package Manager Console.
I have attempted to open a Source File on and execute an algorithm relating to the MersenneTwister type within the MathNet namespace.
However, when I try to generate numbers using this algorithm in F# Interactive, I am met with the error:
File1.fs(3,6): error FS0039: The namespace or module 'MathNet' is not defined. Maybe you want one of the following:
Math
Code is as per below:
module File1
open MathNet.Numerics.Random
let mersenneTwister = new MersenneTwister(42)
let a = mersenneTwister.NextDouble()
Apologies if this is unclear, I am relatively new to F# :)
Are you per change using the interactive window or running an fsx script? Cause these don't recognize your packages.
When reproducing your problem in a F# console app I got this output: Hello 0.374540.
I installed the MathNet.Numerics.fsharp nuget package (which also uses the package you mention) and used the following code in the Program.fs:
open MathNet.Numerics.Random
let hello () =
let mersenneTwister = new MersenneTwister(42)
let a = mersenneTwister.NextDouble()
printfn "Hello %f" a
[<EntryPoint>]
let main argv =
hello ()
0 // return an integer exit code
If you do want to use the nuget package from a script you can reference it in the top of your script like so (absolute or relative)
#r #"C:\Path\bin\Debug\netcoreapp3.0\MathNet.Numerics.dll"
I'm trying to use FAKE to build my F# project.
The build.fsx looks like below and works fine.
#r "packages/FAKE/tools/FakeLib.dll"
open Fake
Target "Default" (fun _ ->
trace "Hello World from FAKE"
)
RunTargetOrDefault "Default"
Then I want to use fsc from FAKE. Following the official tutorial, I added one line open Fake.FscHelper and get below error message:
#r "packages/FAKE/tools/FakeLib.dll"
open Fake
open Fake.FscHelper
// this value is not a function and can not be applied
// union case FscParam.Target: TargetType -> FscParam
Target "Default" (fun _ ->
~~~~~~~~~~~~~~~~
trace "Hello World from FAKE"
)
RunTargetOrDefault "Default"
I appreciate if anyone can give me any advice.
I'm using VS Code on Mac with Mono 4.2.1.
And my paket.lock looks like below:
NUGET
remote: https://www.nuget.org/api/v2
specs:
FAKE (4.21.0)
FSharp.Core (4.0.0.1)
FsUnit (2.0.0)
FSharp.Core (>= 3.1.2.5)
NUnit (3.0.1)
NUnit (3.0.1)
This happens because the module FscHelper defines a constructor called Target (see source), and that constructor conflicts with the Target function from the TargetHelper module. There is an issue filed about it.
Until the issue is fixed, there are three ways to work around this ambiguity:
Don't open FscHelper, just use all its innards in a qualified manner (e.g. FscHelper.Compile etc.)
Re-alias the TargetHelper.Target function in the local scope:
open Fake
open Fake.FscHelper
let Target = TargetHelper.Target
Target "Default" (fun _ ->
trace "Hello World from FAKE"
)
Reorder the open statements:
open Fake.FscHelper
open Fake
And since you're using this helper, note that the documentation for it is outdated. In particular, the Fsc task is deprecated in favor of the Compile task (see source).
Change the order of the open statements
#r #"packages/FAKE/tools/FakeLib.dll"
open Fake.FscHelper
open Fake
Target "a" (fun _ ->
["a.fs"] |> Compile []
The order of your open statements determines the precedence of the name resolution with the later opened modules and namespaces taking precedent.
I am getting this when trying to parse a simple csv string. I am running F# out of VS 2013, the dll says it is version 4.3.0.1 which I thought was F# 3.1. My Fsharp.Data dll is 1.1.10.
I am trying to run this as part of an nunit test using resharper. The snippet does work in interactive mode.
Here is the code:
open FSharp.Data
type TestCsv = CsvProvider<"test,taht\n1,1">
let x = TestCsv.Parse "test,taht\n1,1"
let tests = x.Data |> Seq.map (fun x -> x.test)
tests |> Seq.head
And the result:
System.Exception : Couldn't parse row 1 according to schema: Method not found: 'Microsoft.FSharp.Core.FSharpOption`1<System.String> FSharp.Data.RuntimeImplementation.Operations.AsOption(System.String)'.
Any ideas how to fix this?
FSharp.Data 1.1.10 doesn't support F# 3.1/VS2013. Please try with the prerelease version 2.0.0-alpha3 and let us if that works. Make sure both the unit test project and the library project are using the same version of FSharp.Core (either 4.3.0.0 or 4.3.1.0)
I've written a dummy http server as an exercise in F#.
I'm using Mono 2.4.4 on Ubuntu 10.04 x86_64, with MonoDevelop.
The following code fails to compile with the error:
Error FS0039: The field, constructor or member 'Spawn' is not defined (FS0039)
Could someone try this in VisualStudio please, I don't know whether this is a Mono problem, or my problem.
I have tried several Async examples from the F# book, and they also all produce similar messages about Async.* methods.
Thanks,
Chris.
#light
open System
open System.IO
open System.Threading
open System.Net
open System.Net.Sockets
open Microsoft.FSharp.Control.CommonExtensions
printfn "%s" "Hello World!"
let headers = System.Text.Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: 37\r\nDate: Sun, 13 Jun 2010 05:30:00 GMT\r\nServer: FSC/0.0.1\r\n\r\n")
let content = System.Text.Encoding.ASCII.GetBytes("<html><body>Hello World</body></html>")
let serveAsync (client : TcpClient) =
async { let out = client.GetStream()
do! out.AsyncWrite(headers)
do! Async.Sleep 3000
do! out.AsyncWrite(content)
do out.Close()
}
let http_server (ip, port) =
let server = new TcpListener(IPAddress.Parse(ip),port)
server.Start()
while true do
let client = server.AcceptTcpClient()
printfn "new client"
Async.Spawn (serveAsync client)
http_server ("0.0.0.0", 1234)
Spawn is now called Start (the library APIs have changed a bit since a couple of the books were published a few years ago).
Check the docs at
http://msdn.microsoft.com/en-us/library/ee370232.aspx