I want to have this class serialized, so i can send it as the body of a httpresponsemessage:
[DataContract]
public class Subscription : TableServiceEntity
{
[DataMember]
public string SubscriptionId { get; set; }
[DataMember]
public bool IsAdmin { get; set; }
}
The way i want to use it:
IList<Subscription> subs = ... ;
return new HttpResponseMessage<IList<Subscription>>(subs);
It compiles, but when i run this part i get an error saying the IList can’t be serialized and i have to add it to the known type collection.
I’m guessing the members of TableServiceEntity are not serializable and that’s why i can’t serialize the whole list in all, but i don’t know how to resolve this issue.
Any ideas?
Sincerely,
Zoli
MODIFICATION
I added a new class just as it was said in the first comment and it looks like this:
[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; }
}
I still get the error saying to
add type to known type collection and to use the serviceknowntypeattribute before the operations
the only operation i use is this:
public class DBModelServiceContext : TableServiceContext
{
public DBModelServiceContext(string baseAddress, StorageCredentials credentials)
: base(baseAddress, credentials) { }
public IList<Subscription> Subscriptions
{
get
{
return this.CreateQuery<Subscription>("Subscriptions").ToArray();
}
}
}
MODIFICATION 2
MY interface looks like this:
[OperationContract]
[ServiceKnownType(typeof(IList<Subscription>))]
[WebGet(UriTemplate = "subscription/{value}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
HttpResponseMessage<IList<Subscription>> GetSubscription(string value);
the implementation behind is:
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);}
I’ve managed to find the solution:
The selfmade class( in my case: Subscription) has to be inherited from a custom TableServiceEntity class, which has datacontract, datamember and knowntype attributes:
And the key is that: You don’t send the result back as an HttpResponseMessage
It will always be an HttpResponseMessage no matter what your return type is. If your return type is an IList, then it will be serialized and will be put in the response’s body automatically. So just send it back the way it is. If you want to modify the statuscode of the reply, you can get it done like this:
You can get/set everything from the request/reponse body/header from
WebOperationContext.Current
I hope it helps!
Addition:
But be aware, that if you get your selfmadeclass(in my case subscription) type of object from the azure database, you won’t be able to delete it:
instead the return type has to something that is inherited from the TableServiceEntity, and you can only delete that.
Please, if I’m wrong, update this post!