このクラスをシリアル化したいので、httpresponsemessageの本文として送信できます。
[DataContract]
public class Subscription : TableServiceEntity
{
[DataMember]
public string SubscriptionId { get; set; }
[DataMember]
public bool IsAdmin { get; set; }
}
私がそれを使いたい方法:
IList<Subscription> subs = ... ;
return new HttpResponseMessage<IList<Subscription>>(subs);
コンパイルされますが、この部分を実行すると、IListをシリアル化できないというエラーが表示され、既知のタイプのコレクションに追加する必要があります。TableServiceEntityのメンバーはシリアル化できないと思います。そのため、リスト全体をシリアル化することはできませんが、この問題を解決する方法がわかりません。
何か案は?
心から、
ゾリ
変形
最初のコメントで述べたように、新しいクラスを追加しました。次のようになります。
[DataServiceEntity]
[DataContract]
[KnownType(typeof(Subscription))]
public abstract class SerializableTableServiceEntity
{
[DataMember]
public string PartitionKey { get; set; }
[DataMember]
public string RowKey { get; set; }
[DataMember]
public DateTime Timestamp { get; set; }
}
[DataContract]
public class Subscription : SerializableTableServiceEntity
{
[DataMember]
public string SubscriptionId { get; set; }
[DataMember]
public bool IsAdmin { get; set; }
}
まだエラーが発生します
add type to known type collection and to use the serviceknowntypeattribute before the operations
私が使用する唯一の操作はこれです:
public class DBModelServiceContext : TableServiceContext
{
public DBModelServiceContext(string baseAddress, StorageCredentials credentials)
: base(baseAddress, credentials) { }
public IList<Subscription> Subscriptions
{
get
{
return this.CreateQuery<Subscription>("Subscriptions").ToArray();
}
}
}
変更2
私のインターフェースは次のようになります。
[OperationContract]
[ServiceKnownType(typeof(IList<Subscription>))]
[WebGet(UriTemplate = "subscription/{value}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
HttpResponseMessage<IList<Subscription>> GetSubscription(string value);
背後にある実装は次のとおりです。
public HttpResponseMessage<IList<Subscription>> GetSubscription(string value)
{
var account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
var context = new DBModelServiceContext(account.TableEndpoint.ToString(), account.Credentials);
IList<Subscription> subs = context.Subscriptions;
return new HttpResponseMessage<IList<Subscription>>(subs);}