2011-02-02 174 views
10

這是我的問題。其限定順序的類有一個叫做PaymentStatus屬性,它是一個enum定義像這樣:Azure表存儲,WCF服務和枚舉

public enum PaymentStatuses : int 
    { 
     OnDelivery = 1, 
     Paid = 2, 
     Processed = 3, 
     Cleared = 4 
    } 

,後來,在類本身,屬性定義是非常簡單的:

public PaymentStatuses? PaymentStatus { get; set; } 

但是,如果我嘗試保存以便在Azure表存儲時,我得到以下異常:

System.InvalidOperationException: The type Order+PaymentStatuses' has no settable properties. 

在這一點上我想USI NG enum是不可能的,但快速谷歌搜索返回此:http://social.msdn.microsoft.com/Forums/en-US/windowsazure/thread/7eb1a2ca-6c1b-4440-b40e-012db98ccb0a

此頁面列出了兩個答案,其中一個似乎忽略的問題,並建議使用Azure存儲的enum是罰款。

現在,我不需要在Azure Table Storage中存儲enum,因此我也可以存儲相應的int,但是,我確實需要在WCF服務中公開此屬性。

我試圖使財產利用getset返回從存儲integerenum,並通過使用我DataContextWritingEntity事件移除天青此屬性,但我爲這個實體的事件之前拿到那個異常被解僱。

在這一點上,我很茫然,我不知道我還能做些什麼來將WCF中的這個屬性作爲enum,但Azure商店只有int

回答

15

不支持Enum。儘管它的定義類似於int,但它並不是Table Storage支持的整型。 Here is the list of types supported.枚舉只是一個帶有面向對象風格的整數的字符串表達式。

您可以將int存儲在表存儲中,然後使用Enum.Parse轉換它。

0

只是建議...

我記得在WCF您有特殊的屬性標記枚舉:http://msdn.microsoft.com/en-us/library/aa347875.aspx

而且,當你聲明PaymentStatuses? PaymentStatus,您聲明Nullable<PaymentStatuses> PaymentStatus? sintax只是語法糖。嘗試刪除?並查看發生了什麼(您可以添加PaymentStatuses.NoSet = 0,因爲Int32的默認值爲0)。

祝你好運。

+0

這是行不通的。 WCF本身沒有問題。如果我創建一個DataContract並在其中定義一個枚舉,那麼它工作正常。我需要的是該枚舉以某種方式與Azure存儲表一起工作。 – Shaamaan 2011-02-14 10:03:33

3

雅我有這個相同的問題 我改變了我的屬性,這是早先枚舉int。現在這個int屬性分析傳入INT所以現在它保存到同一枚舉類型的variale這是

public CompilerOutputTypes Type 
{get; set;} 

被chaged到

private CompilerOutputTypes type; 
public int Type 
{ 
    get {return (int)type;} 
    set { type = (CompilerOutputTypes)value; } 
} 
+1

在我的情況下,這不是一個有效的解決方案:使用枚舉的WCF服務仍然需要使用整數而不是枚舉。 – Shaamaan 2011-08-24 14:02:47

0

Parvs解決方案讓我在正確的軌道上的代碼但我做了一些小調整。

private string _EnumType; 
private EnumType _Type; 

//********************************************* 
//********************************************* 
public string EnumType 
{ 
    get { return _Type.ToString(); } 
    set 
     { 
      _EnumType = value; 
      try 
      { 
       _Type = (EnumType)Enum.Parse(typeof(EnumType), value);  
      } 
      catch (Exception) 
      { 
       _EnumType = "Undefined"; 
       _Type = [mynamespace].EnumType.Undefined;     
      }       
     } 
    } 
11

這裏有一個簡單的解決方法:

public int MyEnumValue { get; set; } //for use by the Azure client libraries only 
[IgnoreProperty] public MyEnum MyEnum 
{ 
    get { return (MyEnum) MyEnumValue; } 
    set { MyEnumValue = (int) value; } 
} 

如果一個簡單的後盾值可能已被使用,而不是額外的(!公共)財產這本來是更好 - 沒有麻煩覆蓋ReadEntity的/當然是WriteEntity。我打開了一個便於使用的user voice ticket,因此您可能想要加註它。

0

我也碰到過類似的問題,並實現了一個通用對象拼合/ recomposer API,將彙整複雜的實體爲平EntityProperty字典,使它們可寫表存儲,在DynamicTableEntity形式。

然後,相同的API將從DynamicTableEntityEntityProperty字典中重新構造整個複雜對象。

這是有關您的問題,因爲ObjectFlattenerRecomposer API支持扁平化是通常不寫入到Azure Table中存儲像EnumTimeSpan物業類型,所有Nullable類型,ulonguint將它們轉換成可寫EntityProperties。

該API還處理從拼合EntityProperty字典轉換回原始複雜對象。所有客戶端需要做的就是告訴API,我已經從Azure Table(以DynamicTableEntity.Properties的形式)讀取了這個EntityProperty字典,您可以將它轉換爲此特定類型的對象。 API將重新構造具有所有屬性的完整複雜對象,包括具有原始正確值的「Enum」屬性。

原始對象的所有扁平化和重新組織都是對客戶端(API的用戶)透明地完成的。客戶端無需爲ObjectFlattenerRecomposer API提供任何模式或任何知識來編寫它想要寫入的複雜對象,它只是將該對象作爲「對象」傳遞給API以平滑它。將其轉換回來時,客戶端只需提供想要轉換爲的扁平EntityProperty字典的實際類型的對象。 API的通用ConvertBack方法將簡單重構Type T的原始對象並將其返回給客戶端。

請參閱下面的使用示例。對象不需要實現像'ITableEntity'這樣的接口,也不需要從特定的基類繼承。他們不需要提供一組特殊的構造函數。

博客:https://doguarslan.wordpress.com/2016/02/03/writing-complex-objects-to-azure-table-storage/

NuGet包:https://www.nuget.org/packages/ObjectFlattenerRecomposer/

用法:

//Flatten object (ie. of type Order) and convert it to EntityProperty Dictionary 
Dictionary<string, EntityProperty> flattenedProperties = EntityPropertyConverter.Flatten(order); 

// Create a DynamicTableEntity and set its PK and RK 
DynamicTableEntity dynamicTableEntity = new DynamicTableEntity(partitionKey, rowKey); 
dynamicTableEntity.Properties = flattenedProperties; 

// Write the DynamicTableEntity to Azure Table Storage using client SDK 

//Read the entity back from AzureTableStorage as DynamicTableEntity using the same PK and RK 
DynamicTableEntity entity = [Read from Azure using the PK and RK]; 

//Convert the DynamicTableEntity back to original complex object. 
Order order = EntityPropertyConverter.ConvertBack<Order>(entity.Properties);