1

我想让这个类序列化,所以我可以将它作为 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);}
4

2 回答 2

2

我设法找到了解决方案:

自制类(在我的例子中:订阅)必须从自定义 TableServiceEntity 类继承,该类具有 datacontract、datamember 和 knowntype 属性:

[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; }
    }

关键是:您不会将结果作为 HttpResponseMessage 发回, 无论您的返回类型是什么,它都将始终是 HttpResponseMessage。如果您的返回类型是 IList,那么它将被序列化并自动放入响应的正文中。所以就按原样寄回去吧。如果要修改回复的状态码,可以这样完成:

WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Forbidden;

您可以从请求/响应正文/标头中获取/设置所有内容

WebOperationContext.Current

我希望它有帮助!

添加:

但请注意,如果您从 azure 数据库中获取 selfmadeclass(在我的情况下为订阅)类型的对象,您将无法删除它:

Subscription s = (from e in this.CreateQuery<Subscription>("Subscriptions")
                                       where e.SubscriptionId == subscriptionID
                                       select e).FirstOrDefault();
context.DeleteObject(s); //THIS WILL THROW AN EXCEPTION

相反,返回类型必须是从 TableServiceEntity 继承的东西,您只能删除它。

请,如果我错了,请更新此帖子!

于 2012-05-17T08:16:42.143 回答
1

您应该创建自己的类并告诉 azure 使用它,而不是从 TableServiceEntity 继承:

[DataServiceEntity]
[DataContract]
public class MyClass
{
    [DataMember]
    public string PartitionKey { get; set; }

    [DataMember]
    public string RowKey { get; set; }

    [DataMember]
    public DateTime Timestamp { get; set; }
}

关键点是DataServiceEntity属性,它允许您将此类与 azure 一起使用。

于 2012-05-15T16:19:49.437 回答