Last active
October 25, 2015 18:12
-
-
Save kunjee17/3e6d4c6f263a3c543b89 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
[<CLIMutableAttribute>] | |
[<AliasAttribute("hello")>] | |
type HelloDb = | |
{ Name : string } | |
[<LiteralAttribute>] | |
let connStr = "Server = localhost; Port = 5432; Database = database; User Id = username; password = password;" | |
let dbfactory = OrmLiteConnectionFactory(connStr, PostgreSqlDialect.Provider) | |
let agent = | |
MailboxProcessor.Start(fun inbox -> | |
//let db = dbfactory.Open(); persistance open connection. | |
let rec messageLoop() = | |
async { | |
let! (msg : Hello) = inbox.Receive() | |
do use db = dbfactory.Open() | |
//do some processing | |
db.InsertAsync(msg) | |
|> Async.AwaitTask | |
|> ignore | |
printfn "%A" msg.Name | |
do! Async.Sleep(rnd.Next(100, 1000)) | |
return! messageLoop() | |
} | |
messageLoop()) | |
agent.Post {Name:"Kunjan"} | |
//How can use this agent to insert data. Because if they are queue I guess it is ok to use persistance open connection. Here I am not doing it | |
//but I guess that very much possible |
@swlaschin and @isaacabraham how can I use do keyword within async keyword. It is giving me compilation error.
If to await an Async, it's do!
@isaacabraham I tried that also. It also didn't work. Can you please give example in context of gist? It will be great help.
@swlaschin and @isaacabraham. I guess I make it working. Please have a look that if it is good as per code. It is working now without closing connection.
And ya we can't use any bang thing in do clause. means that do! will be outside of that clause. Or anything that I should be asynced...
PS: ref - http://stackoverflow.com/questions/7433426/f-async-dispose
Looks good to me. That SO question has good answers too!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As Isaac said, you can use a
using
expression, or alternatively, create a new scope by indenting with ado
at the top, like this:Output is: