4 people like it.

How to Serialize Record, Discriminated Union and Tuple in Azure?

Here is the implementation of the three types in WCF Azure, and a sample using WCF in worker role, a WPF test project to invoke the three kinds of data type from worker role. Link is http://fsharp3sample.codeplex.com/SourceControl/changeset/view/13876

 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: 
open System.ServiceModel
open System.Runtime.Serialization
/// Record for LoanInformation
[<DataContract>]
type LoanInformation =
    { [<DataMember>] mutable Amount : double
      [<DataMember>] mutable InterestRateInPercent : double 
      [<DataMember>] mutable TermInMonth : int }

/// Record for PaymentInformation
[<DataContract>]
type PaymentInformation =
    { [<DataMember>] mutable MonthlyPayment : double
 
      [<DataMember>] mutable TotalPayment : double }

//[<DataContract>] --> Never add this attribute on Discriminated Union, it won't work
/// Discriminated Union
type Suit =
    | Heart
    | Diamond
    | Spade
    | Club

/// For F#, all the interface method must be given a parameter name: eg. a:LoanInformation
/// otherwise, you will fail to start the service host
[<ServiceContract>]
type public ILoanCadulator =
    /// Use Record to send and recieve data
    [<OperationContract>]
    abstract Calculate : a:LoanInformation -> PaymentInformation
    /// Use Tuple to send and recieve data
    [<OperationContract>]
    abstract Calculate2 : b:(int * string) -> int * string
    /// Use Discriminated Union(DU) to send and recieve data
    [<OperationContract>]
    abstract Calculate3 : c: Suit -> Suit
namespace System
namespace System.Runtime
namespace System.Runtime.Serialization
type LoanInformation =
  {mutable Amount: double;
   mutable InterestRateInPercent: double;
   mutable TermInMonth: int;}

Full name: Script.LoanInformation


 Record for LoanInformation
LoanInformation.Amount: double
Multiple items
val double : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.double

--------------------
type double = System.Double

Full name: Microsoft.FSharp.Core.double
LoanInformation.InterestRateInPercent: double
LoanInformation.TermInMonth: int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
type PaymentInformation =
  {mutable MonthlyPayment: double;
   mutable TotalPayment: double;}

Full name: Script.PaymentInformation


 Record for PaymentInformation
PaymentInformation.MonthlyPayment: double
PaymentInformation.TotalPayment: double
type Suit =
  | Heart
  | Diamond
  | Spade
  | Club

Full name: Script.Suit


 Discriminated Union
union case Suit.Heart: Suit
union case Suit.Diamond: Suit
union case Suit.Spade: Suit
union case Suit.Club: Suit
type ILoanCadulator =
  interface
    abstract member Calculate : a:LoanInformation -> PaymentInformation
    abstract member Calculate2 : b:(int * string) -> int * string
    abstract member Calculate3 : c:Suit -> Suit
  end

Full name: Script.ILoanCadulator


 For F#, all the interface method must be given a parameter name: eg. a:LoanInformation
 otherwise, you will fail to start the service host
abstract member ILoanCadulator.Calculate : a:LoanInformation -> PaymentInformation

Full name: Script.ILoanCadulator.Calculate


 Use Record to send and recieve data
abstract member ILoanCadulator.Calculate2 : b:(int * string) -> int * string

Full name: Script.ILoanCadulator.Calculate2


 Use Tuple to send and recieve data
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
abstract member ILoanCadulator.Calculate3 : c:Suit -> Suit

Full name: Script.ILoanCadulator.Calculate3


 Use Discriminated Union(DU) to send and recieve data

More information

Link:http://fssnip.net/c5
Posted:12 years ago
Author:AndrewXue
Tags: f# , azure , serialize