F# interactive, AppFabric cache and PowerShell (Part 1)

By Petr at July 14, 2010 03:29
Filed Under:

Usually you can improve performance and scalability of distributed application using in-memory cache data. Windows Server AppFabric recently released by Microsoft includes distributed cache module formerly known under codename “Velocity”. It is a distributed in-memory application cache platform for developing scalable, available, and high-performance applications. AppFabric fuses memory across multiple computers to give a single unified cache view to applications. Applications can store any serializable CLR object without worrying about where the object gets stored. Scalability can be achieved by simply adding more computers on demand. The cache also allows for copies of data to be stored across the cluster, thus protecting data against failures. It runs as a service accessed over the network.

Installation and configuration went very smoothly, Microsoft documentation on setup and configuration is quite good.

I set up AppFabric on my laptop running 64-bit version of Windows 7 without any problems.

I planned to play with Cache API using F# interactive – a great tool available in Visual Studio 2010 allowing quickly prototype and test F# code.

Data stored in AppFabric cache are partitioned using named memory caches which could be created by PowerShell commands – PowerShell is the main tool for AppFabric administration tasks. Strangely enough PowerShell command is the only way to create new named cache on the server (there is no .NET API for this purpose) so I had to learn how to call PowerShell commands from F#. After some googling I found the great blog post by Stephen Kaufman “Calling PowerShell from .NET” which had exactly what I needed but he used C#. Both C# and F# are .NET languages so it shouldn’t be very hard to translate code from one to another. So I created a couple of functions to call PowerShell commands from F# and tried them in F# interactive.

 

F#, using GeSHi 1.0.8.8
  1. // We need to refer PowerShell API assembly
  2. #r @"C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll"
  3. open System.Management.Automation
  4. open System.Management.Automation.Runspaces
  5. let invokePsCacheCommand cmdName (parNameVal: (string * obj) list) =
  6. let state = InitialSessionState.CreateDefault()
  7. // Import PowerShell AppFabric modules
  8. [|"DistributedCacheAdministration"; "DistributedCacheConfiguration"|] |> state.ImportPSModule
  9. state.ThrowOnRunspaceOpenError <- true
  10. let rs = RunspaceFactory.CreateRunspace(state)
  11. rs.Open()
  12. // Create PowerShell command pipeline
  13. let pipe = rs.CreatePipeline()
  14. pipe.Commands.Add(Command("Use-CacheCluster"))
  15. let cmd = new Command(cmdName)
  16. // Add command parameters as name-value pairs
  17. parNameVal |> List.iter (fun (name, value) -> cmd.Parameters.Add(CommandParameter(name, value)))
  18. pipe.Commands.Add(cmd)
  19. // And invoke commands
  20. pipe.Invoke()
  21. let createCache cacheName =
  22. [("Name", cacheName);
  23. ("Expirable", box false)] |> invokePsCacheCommand "New-Cache"
  24. let removeCache cacheName =
  25. [("Name", cacheName)] |> invokePsCacheCommand "Remove-Cache"
  26. let exportCacheConfig fileName =
  27. [("File", fileName)] |> invokePsCacheCommand "Export-CacheClusterConfig"
Parsed in 0.026 seconds at 51.22 KB/s

 

Now I can try to create my first named cache from F# interactive:

 

createCache "MyCache"

 

I selected this string and hit Alt-Enter to execute it in F# Interactive window and immediately got a cryptic message: “The specified module 'DistributedCacheAdministration' was not loaded because no valid module file was found in any module directory.” After a short internet search I found this post by Ron Jacobs. It seems that I’ve got this error because I’m using 64-bit version of AppFabric but VisualStudio runs Fsi.exe as 32-bit process. Am I out of lack and cannot use F# interactive to run PowerShell commands? No. It seems that in quickly expanding F# community one can find the answer to almost any problem:) Here it is: “Making the FSharp interpreter (fsi) run in 64bit!” I made all woodoo magic procedures described in this article and finally got sweet picture in power shell command window:

 

FsiCache

 

In the next part I’ll describe how to use cache API to store and retrieve data using F#.

Comments are closed