2012-09-16 51 views
3

工作代碼位於此問題的底部!使用記錄類型的F#WebApi不提供所需的XML輸出

隨着F#以下的WebAPI控制器:

namespace FsWeb.Controllers 

open System.Web 
open System.Web.Mvc 
open System.Net.Http 
open System.Web.Http 
open Microsoft.FSharp.Linq; 
open Microsoft.FSharp.Data.TypeProviders; 

type dbSchema = SqlDataConnection<"conn string"> 

[<CLIMutable>] 
type ViewModelRec = { CustomerId : string } 

type ValuesController() = 
    inherit ApiController() 
    let db = dbSchema.GetDataContext() 
    do 
     db.DataContext.Log <- System.Console.Out 

    // GET /api/values 
    member x.Get(take:int, skip:int) = 
     let t = take 
     let s = skip 

     query { 
      for row in db.Reservations do 
      skip s 
      take t 
      select { CustomerId = row.CustomerId.ToString() } 
     } 

XML輸出是:

<ArrayOfViewModelRec xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/FsWeb.Controllers"> 
<ViewModelRec> 
<CustomerId_x0040_>A98971F0-5F93-4F0D-B5AC-95E161458381</CustomerId_x0040_> 
</ViewModelRec> 
<ViewModelRec> 
<CustomerId_x0040_>60DF7CF6-91FB-4BBE-AD6B-FE0CAA32F60E</CustomerId_x0040_> 
</ViewModelRec> 
<ViewModelRec> 
<CustomerId_x0040_>84506E40-139C-4DD1-B396-3CE13DC20996</CustomerId_x0040_> 
</ViewModelRec> 
<ViewModelRec> 
<CustomerId_x0040_>D9FABC12-9B19-46C2-9776-E31171A302D5</CustomerId_x0040_> 
</ViewModelRec> 
<ViewModelRec> 
<CustomerId_x0040_>B1B6C617-AA0E-46A9-A0B6-312A229BE178</CustomerId_x0040_> 
</ViewModelRec> 
<ViewModelRec> 
<CustomerId_x0040_>BAB194AE-CD2C-400F-B81E-2A293C50C404</CustomerId_x0040_> 
</ViewModelRec> 
<ViewModelRec> 
<CustomerId_x0040_>AE001AB4-8410-43BF-9C93-AE5A2EACFDF5</CustomerId_x0040_> 
</ViewModelRec> 
<ViewModelRec> 
<CustomerId_x0040_>2DDBDB91-9307-4BF7-865F-C6237D09D542</CustomerId_x0040_> 
</ViewModelRec> 
<ViewModelRec> 
<CustomerId_x0040_>A3E93E5C-2B7E-4B8D-8AAD-AD245E32093F</CustomerId_x0040_> 
</ViewModelRec> 
<ViewModelRec> 
<CustomerId_x0040_>B64B814F-9B4C-46BD-A804-648309FA7DD0</CustomerId_x0040_> 
</ViewModelRec> 
</ArrayOfViewModelRec> 

當我使用一個類來代替,命名看起來是正確的(不x0040標籤)。

任何人的建議?

更新:解決方法:

namespace FsWeb.Controllers 

open System.Web 
open System.Web.Mvc 
open System.Net.Http 
open System.Web.Http 
open System.Runtime.Serialization; 
open Microsoft.FSharp.Linq; 
open Microsoft.FSharp.Data.TypeProviders; 

type dbSchema = SqlDataConnection<"here comes the connection string"> 

[<DataContract>] 
type ViewModelRec = { 
    [<field: DataMember(Name="CustomerId")>] 
    CustomerId : string } 

type ValuesController() = 
    inherit ApiController() 
    let db = dbSchema.GetDataContext() 
    do 
     db.DataContext.Log <- System.Console.Out 

    member x.Get(take:int, skip:int) = 
     let t = take 
     let s = skip 

     query { 
      for row in db.Reservations do 
      skip s 
      take t 
      select { CustomerId = row.CustomerId.ToString() } 
     } 

回答

6

我不是F#專家,但據我所知,記錄類型的屬性獲取由同名的內部字段,只有支持「@」最後。在你的情況「CustomerId @」。

然後,序列化程序會選擇序列化這些支持字段,而不是公共getter,與您在其上具有[Serializable]屬性的C#類相同。

當您使用DataContract /數據成員它應該工作 - 看F# Serialization of Record types

+0

這無疑解決了我的問題!我會將你的評論標記爲答案。這也解釋了爲什麼使用類的工作正常,因爲那時我正在定義屬性get; set;被使用。 –