3 people like it.

Using Azure Table Storage

Using Azure Table Storage with WindowsAzure.Storage

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
open Microsoft.WindowsAzure.Storage
open Microsoft.WindowsAzure.Storage.Table
open Microsoft.WindowsAzure.ServiceRuntime

type Person(partitionKey, rowKey, name) =
    inherit TableEntity(partitionKey, rowKey)
    new(name) = Person("defaultPartition", System.Guid.NewGuid().ToString(), name)
    new() = Person("")
    member val Name = name with get, set

(*
    Add this into ServiceDefinition.csdef <WorkerRole>:
    <ConfigurationSettings>
      <Setting name="TableStorageConnectionString" />
    </ConfigurationSettings>

    Add this into ServiceConfiguration.*.csdef <ConfigurationSettings>:
    <Setting name="TableStorageConnectionString" value="UseDevelopmentStorage=true" />
*)

let doAction tableName operation = 
    let account = 
        "TableStorageConnectionString"
        |> RoleEnvironment.GetConfigurationSettingValue 
        |> CloudStorageAccount.Parse

    //memoize this
    let client = account.CreateCloudTableClient()
    let table = client.GetTableReference(tableName)

    async {
        let! created = table.CreateIfNotExistsAsync() |> Async.AwaitTask
        return! table.ExecuteAsync(operation) |> Async.AwaitTask
    }

let CreatePerson() = 
    Person("Tuomas")
    |> TableOperation.Insert //Insert, Delete, Replace, etc.
    |> doAction "MyStorageTable"
    |> Async.RunSynchronously
namespace Microsoft
namespace Microsoft.WindowsAzure
namespace Microsoft.WindowsAzure.Storage
namespace Microsoft.WindowsAzure.Storage.Table
Multiple items
type Person =
  inherit TableEntity
  new : unit -> Person
  new : name:string -> Person
  new : partitionKey:string * rowKey:string * name:string -> Person
  member Name : string
  member Name : string with set

Full name: Script.Person

--------------------
new : unit -> Person
new : name:string -> Person
new : partitionKey:string * rowKey:string * name:string -> Person
val partitionKey : string
val rowKey : string
val name : string
Multiple items
type TableEntity =
  new : unit -> TableEntity + 1 overload
  member ETag : string with get, set
  member PartitionKey : string with get, set
  member ReadEntity : properties:IDictionary<string, EntityProperty> * operationContext:OperationContext -> unit
  member RowKey : string with get, set
  member Timestamp : DateTimeOffset with get, set
  member WriteEntity : operationContext:OperationContext -> IDictionary<string, EntityProperty>
  static member DisableCompiledSerializers : bool with get, set
  static member DisablePropertyResolverCache : bool with get, set
  static member ReadUserObject : entity:obj * properties:IDictionary<string, EntityProperty> * operationContext:OperationContext -> unit
  ...

Full name: Microsoft.WindowsAzure.Storage.Table.TableEntity

--------------------
TableEntity() : unit
TableEntity(partitionKey: string, rowKey: string) : unit
namespace System
Multiple items
type Guid =
  struct
    new : b:byte[] -> Guid + 4 overloads
    member CompareTo : value:obj -> int + 1 overload
    member Equals : o:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member ToByteArray : unit -> byte[]
    member ToString : unit -> string + 2 overloads
    static val Empty : Guid
    static member NewGuid : unit -> Guid
    static member Parse : input:string -> Guid
    static member ParseExact : input:string * format:string -> Guid
    ...
  end

Full name: System.Guid

--------------------
System.Guid()
System.Guid(b: byte []) : unit
System.Guid(g: string) : unit
System.Guid(a: int, b: int16, c: int16, d: byte []) : unit
System.Guid(a: uint32, b: uint16, c: uint16, d: byte, e: byte, f: byte, g: byte, h: byte, i: byte, j: byte, k: byte) : unit
System.Guid(a: int, b: int16, c: int16, d: byte, e: byte, f: byte, g: byte, h: byte, i: byte, j: byte, k: byte) : unit
System.Guid.NewGuid() : System.Guid
val set : elements:seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
val doAction : tableName:string -> operation:TableOperation -> Async<TableResult>

Full name: Script.doAction
val tableName : string
val operation : TableOperation
val account : CloudStorageAccount
Multiple items
type CloudStorageAccount =
  new : storageCredentials:StorageCredentials * useHttps:bool -> CloudStorageAccount + 4 overloads
  member BlobEndpoint : Uri
  member BlobStorageUri : StorageUri with get, set
  member CreateCloudAnalyticsClient : unit -> CloudAnalyticsClient
  member CreateCloudBlobClient : unit -> CloudBlobClient
  member CreateCloudFileClient : unit -> CloudFileClient
  member CreateCloudQueueClient : unit -> CloudQueueClient
  member CreateCloudTableClient : unit -> CloudTableClient
  member Credentials : StorageCredentials with get, set
  member FileEndpoint : Uri
  ...

Full name: Microsoft.WindowsAzure.Storage.CloudStorageAccount

--------------------
CloudStorageAccount(storageCredentials: Auth.StorageCredentials, useHttps: bool) : unit
CloudStorageAccount(storageCredentials: Auth.StorageCredentials, endpointSuffix: string, useHttps: bool) : unit
CloudStorageAccount(storageCredentials: Auth.StorageCredentials, accountName: string, endpointSuffix: string, useHttps: bool) : unit
CloudStorageAccount(storageCredentials: Auth.StorageCredentials, blobEndpoint: System.Uri, queueEndpoint: System.Uri, tableEndpoint: System.Uri, fileEndpoint: System.Uri) : unit
CloudStorageAccount(storageCredentials: Auth.StorageCredentials, blobStorageUri: StorageUri, queueStorageUri: StorageUri, tableStorageUri: StorageUri, fileStorageUri: StorageUri) : unit
CloudStorageAccount.Parse(connectionString: string) : CloudStorageAccount
val client : CloudTableClient
CloudStorageAccount.CreateCloudTableClient() : CloudTableClient
val table : CloudTable
CloudTableClient.GetTableReference(tableName: string) : CloudTable
val async : AsyncBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
val created : bool
CloudTable.CreateIfNotExistsAsync() : System.Threading.Tasks.Task<bool>
CloudTable.CreateIfNotExistsAsync(cancellationToken: System.Threading.CancellationToken) : System.Threading.Tasks.Task<bool>
CloudTable.CreateIfNotExistsAsync(requestOptions: TableRequestOptions, operationContext: OperationContext) : System.Threading.Tasks.Task<bool>
CloudTable.CreateIfNotExistsAsync(requestOptions: TableRequestOptions, operationContext: OperationContext, cancellationToken: System.Threading.CancellationToken) : System.Threading.Tasks.Task<bool>
Multiple items
type Async
static member AsBeginEnd : computation:('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit)
static member AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate)
static member AwaitIAsyncResult : iar:IAsyncResult * ?millisecondsTimeout:int -> Async<bool>
static member AwaitTask : task:Task<'T> -> Async<'T>
static member AwaitWaitHandle : waitHandle:WaitHandle * ?millisecondsTimeout:int -> Async<bool>
static member CancelDefaultToken : unit -> unit
static member Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
static member FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg:'Arg1 * beginAction:('Arg1 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * beginAction:('Arg1 * 'Arg2 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * arg3:'Arg3 * beginAction:('Arg1 * 'Arg2 * 'Arg3 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromContinuations : callback:(('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T>
static member Ignore : computation:Async<'T> -> Async<unit>
static member OnCancel : interruption:(unit -> unit) -> Async<IDisposable>
static member Parallel : computations:seq<Async<'T>> -> Async<'T []>
static member RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:CancellationToken -> 'T
static member Sleep : millisecondsDueTime:int -> Async<unit>
static member Start : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions * ?cancellationToken:CancellationToken -> Task<'T>
static member StartChild : computation:Async<'T> * ?millisecondsTimeout:int -> Async<Async<'T>>
static member StartChildAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions -> Async<Task<'T>>
static member StartImmediate : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartWithContinuations : computation:Async<'T> * continuation:('T -> unit) * exceptionContinuation:(exn -> unit) * cancellationContinuation:(OperationCanceledException -> unit) * ?cancellationToken:CancellationToken -> unit
static member SwitchToContext : syncContext:SynchronizationContext -> Async<unit>
static member SwitchToNewThread : unit -> Async<unit>
static member SwitchToThreadPool : unit -> Async<unit>
static member TryCancelled : computation:Async<'T> * compensation:(OperationCanceledException -> unit) -> Async<'T>
static member CancellationToken : Async<CancellationToken>
static member DefaultCancellationToken : CancellationToken

Full name: Microsoft.FSharp.Control.Async

--------------------
type Async<'T>

Full name: Microsoft.FSharp.Control.Async<_>
static member Async.AwaitTask : task:System.Threading.Tasks.Task<'T> -> Async<'T>
CloudTable.ExecuteAsync(operation: TableOperation) : System.Threading.Tasks.Task<TableResult>
CloudTable.ExecuteAsync(operation: TableOperation, cancellationToken: System.Threading.CancellationToken) : System.Threading.Tasks.Task<TableResult>
CloudTable.ExecuteAsync(operation: TableOperation, requestOptions: TableRequestOptions, operationContext: OperationContext) : System.Threading.Tasks.Task<TableResult>
CloudTable.ExecuteAsync(operation: TableOperation, requestOptions: TableRequestOptions, operationContext: OperationContext, cancellationToken: System.Threading.CancellationToken) : System.Threading.Tasks.Task<TableResult>
val CreatePerson : unit -> TableResult

Full name: Script.CreatePerson
type TableOperation =
  static member Delete : entity:ITableEntity -> TableOperation
  static member Insert : entity:ITableEntity -> TableOperation + 1 overload
  static member InsertOrMerge : entity:ITableEntity -> TableOperation
  static member InsertOrReplace : entity:ITableEntity -> TableOperation
  static member Merge : entity:ITableEntity -> TableOperation
  static member Replace : entity:ITableEntity -> TableOperation
  static member Retrieve<'TElement> : partitionKey:string * rowkey:string -> TableOperation + 5 overloads

Full name: Microsoft.WindowsAzure.Storage.Table.TableOperation
TableOperation.Insert(entity: ITableEntity) : TableOperation
TableOperation.Insert(entity: ITableEntity, echoContent: bool) : TableOperation
static member Async.RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:System.Threading.CancellationToken -> 'T
Raw view Test code New version

More information

Link:http://fssnip.net/nR
Posted:9 years ago
Author:Tuomas Hietanen
Tags: azure