Skip to content

Show how to read large field values (e.g. strings, binary values) into streams with ODataMessageReader #337

@habbes

Description

@habbes

When you have fields which large values, usually strings or binary data encoded as base64 strings, deserializing these fields might lead to large heap allocations due to reading the entire value into memory. ODataMessageWriter exposes a ReadAsStreamFunc property that the user can assign a delegate that returns true for any property that should be deserialized into a stream. This feature is not documented but is useful work performance-sensitive services working which large payloads.

Basically, there are two steps:

  • Register a ReadAsStreamFunc on the message writer settings which return true for the properties that you want to stream. ODL will call your function for each property and, if you return true, will enter a new reader state ODataReaderState.Stream.
  • Handle the ODataReaderState.Stream state by creating stream or text reader, depending on the type of the property, and read the stream.

Here's a test that demonstrates the feature: https://github.com/OData/odata.net/blob/release-7.x/test/FunctionalTests/Microsoft.OData.Core.Tests/JsonLight/ODataJsonLightReaderTests.cs#L846

We should add a tutorial article this and/or mention it in the performance guidelines

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions