I get a 'SerializationContext is required. Use SerializationScope to set it.' when trying to return IQueryable in a web.api controller:

       [GET("frontpage/section")]
    public IQueryable<NNet.Model.Content.Article> Get() {
        try {
             return Query.All<NNet.Model.Content.Article>().Where(t => t.CurrentState != NNet.Model.Content.Article.StateType.Deleted); 
        } catch (Exception ex) {
            Log.ErrorException("Error processing get Frontpage Articles", ex);
        }
        return null;
    }

ExceptionMessage:The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.

ExceptionType:System.InvalidOperationException

InnerException:{

ExceptionMessage:SerializationContext is required. Use SerializationScope to set it.

ExceptionType:System.InvalidOperationException

StackTrace:

at Xtensive.Orm.Serialization.SerializationContext.Demand()

at Xtensive.Orm.Entity.System.Runtime.Serialization.ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)

at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeISerializable(JsonWriter writer, ISerializable value, JsonISerializableContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)

at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)

at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)

at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)

at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value)

at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value) at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value) at System.Net.Http.Formatting.JsonMediaTypeFormatter.<>cDisplayClassd.<writetostreamasync>bc()

at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)

asked Feb 24 '13 at 10:11

Tony's gravatar image

Tony
53262628


One Answer:

Hello Tony,

as the exception suggests you need active serialization context to use built-in DataObjects.Net serialization. Unfortunately it has some deficiencies thus we recommend to use Json.NET built-in capabilities instead.

By default Json.NET will use ISerializable.GetObjectData method if it is implemented. To avoid that annotate your entity with [JsonObject(MemberSerialization.OptIn)] and persistent properties with [JsonProperty]. This will instruct Json.NET to use its own serialization.

It's also possible to write a custom JsonConverter that will deal with any DataObjects.Net entity in a generic fashion. You could inspect persistent fields using TypeInfo property of a particular entity and use indexer to access property values.

answered Feb 25 '13 at 12:13

Denis%20Krjuchkov's gravatar image

Denis Krjuchkov
179325

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

Subscription:

Once you sign in you will be able to subscribe for any updates here

Tags:

×1

Asked: Feb 24 '13 at 10:11

Seen: 2,834 times

Last updated: Feb 25 '13 at 12:13

Related questions

powered by OSQA