The Azurite open-source emulator provides a free local environment for testing your Azure blob, queue storage, and table storage applications.
That quote from the Microsoft docs page on Azurite gets across what Azurite is about. In this post, I'll show you how you can run it using NPM and start it automatically during local SAFE Stack development.
Run Azurite using NPM
Add to ./package.json
:
"scripts": {
...
"azurite": "azurite -s -l ./.azurite --loose"
...
},
"devDependencies": {
...
"azurite": "3.18.0",
...
}
- The addition to the
"devDependencies"
section ensures that azurite is available to you after you've runnpm install
. - The addition to the
"scripts"
section means that you can usenpm run azurite
to run azurite.
As mentioned in a previous post, you could also run Azurite using Docker if you prefer.
Interact with Azurite
- Start Azurite using
npm run azurite
(after making the above changes topackage.json
) -
Run the following in FSI
#r "nuget: Azure.Storage.Queues,12.11.0" open Azure.Storage.Queues let queueServiceClient = QueueServiceClient "UseDevelopmentStorage=true" let queueClient = queueServiceClient.GetQueueClient("blog-queue") // Create the queue. queueClient.CreateIfNotExists() // Print the contents of a message. // Will see nothing as no message has been added to the queue. queueClient.ReceiveMessages().Value |> Array.iter (fun msg -> printfn $"{msg.Body}") // Send a message. queueClient.SendMessage("Hello Queue!") // Receive a message. // Will see the message that has just been added. queueClient.ReceiveMessages().Value |> Array.iter (fun msg -> printfn $"{msg.Body}")
For good examples of what else you can do, see Ryan's post on using Azure Queues with F# and Kash's post on using Azure Blob Storage with F#.
Run Azurite as part of app development
Add to Build.fs
:
let runAzurite () =
Directory.create "./.azurite"
// This doesn't return until azurite is shut down, so we can't await it / run sync.
// Using 'runParallel' to get coloured process name against output.
async { runParallel [ "azurite", npm "run azurite" "." ] } |> Async.Start
let queueServiceClient = QueueServiceClient "UseDevelopmentStorage=true"
queueServiceClient.GetQueueClient("blog-queue").CreateIfNotExists () |> ignore
Target.create "Run" (fun _ ->
...
runAzurite ()
...
)
- Azurite will now be run when we start our SAFE app using
dotnet run
, and it will have a "blog-queue" queue available.
With that done, you can follow Ryan's previous post on using WebJobs to get started interacting with queues. To make sure that your app is interacting with Azurite, you'll need to configure the app to use the connection string "UseDevelopmentStorage=true"
locally.
Wrap up
My experience with Azurite has been that it does exactly what you'd want it to: it just works and behaves as though you're interacting with Azure storage. With the above easy steps, you can get Azurite running as part of your regular SAFE Stack development, easing the development of apps that interact with Azure storage.