Table of Contents

OhData.Client

A typed .NET client for OData 4.0 services. Provides a fluent, LINQ-style API for querying and mutating entity sets - no code generation required.

This guide is split across several pages:

  • Overview & setup (this page) — installation, constructing the client, IHttpClientFactory, entity set name resolution, and client options.
  • Querying — $filter, $select, $expand, $orderby, $top/$skip, IncludeCount.
  • Terminal operations — ToListAsync, ToPageAsync, FirstOrDefaultAsync, CountAsync, AnyAsync, and the rest, plus the annotation-preserving reads that surface a nested {Nav}@odata.nextLink.
  • Single-entity operations — get, insert, replace, partial update, delete, ETags, and conditional GET.
  • Error handling & literal types — ODataClientException and the CLR-to-OData literal mapping.

Installation

dotnet add package EnGen.OhData.Client

Setup

// Create directly (owns the HttpClient - dispose when done)
var client = new OhDataClient("https://api.example.com/odata");

// Or wrap a caller-supplied HttpClient (recommended for IHttpClientFactory)
var client = new OhDataClient(httpClient);

// With custom JSON options — e.g. opt into camelCase for a camelCase-configured server
// (the default is PascalCase, matching the OhData server's default $metadata/responses)
var client = new OhDataClient(httpClient, new OhDataClientOptions
{
    JsonOptions = new JsonSerializerOptions
    {
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
        // ...
    }
});

With IHttpClientFactory (ASP.NET Core)

// Program.cs
builder.Services.AddHttpClient("products", c =>
    c.BaseAddress = new Uri("https://api.example.com/odata/"));

// In a service:
public class ProductService(IHttpClientFactory factory)
{
    private readonly OhDataClient _client = new(factory.CreateClient("products"));
}

Entity set name resolution

The client resolves the entity set name automatically via:

  1. [ODataEntitySet("CustomName")] attribute on the model class, or
  2. Simple pluralisation of the class name (Product → Products, Category → Categories)
[ODataEntitySet("MyCategories")]  // overrides pluralisation
public class Category { ... }

Pass an explicit name to For<T> if needed:

client.For<Category>("MyCategories")

Client options

OhDataClientOptions is passed to the OhDataClient constructor to customise behaviour.

JsonOptions

Applied to both request serialization and response deserialization. Defaults: PascalCase naming (PropertyNamingPolicy = null, matching the OhData server's PascalCase default), case-insensitive reads, null values omitted on write. Set PropertyNamingPolicy = JsonNamingPolicy.CamelCase to target a camelCase server.

var client = new OhDataClient(httpClient, new OhDataClientOptions
{
    JsonOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web)
    {
        Converters = { new JsonStringEnumConverter() }
    }
});

NotFoundBehavior

Controls how 404 Not Found responses are handled for single-entity GET operations (GetAsync, GetWithETagAsync). Default is NotFoundBehavior.ReturnNull.

Value Behaviour
ReturnNull (default) Returns null when the entity is not found
Throw Throws ODataClientException with status 404
var client = new OhDataClient(httpClient, new OhDataClientOptions
{
    NotFoundBehavior = NotFoundBehavior.Throw
});

Whether the @odata.nextLink walker (ToListAsync, ToAsyncEnumerable, ToAnnotatedAsyncEnumerable) may follow a link whose origin — scheme, host and port — differs from the HttpClient.BaseAddress. Default false: a cross-origin link fails the read with InvalidOperationException and no request is made.

A nextLink is a URL named in a response body, and the walker builds a fresh request for it — so the HttpClient's DefaultRequestHeaders, Authorization among them, get attached to whatever host that body names. This is not a redirect, so HttpClientHandler's cross-origin credential stripping never runs. A compromised server, a caching proxy or a MITM on a plaintext hop can therefore point the next hop at a host of its choosing and be handed your bearer token.

The default refuses the hop rather than stripping Authorization, because the credential at risk is not only that header: a default-header API key (X-Api-Key, Ocp-Apim-Subscription-Key, …) leaks identically, and dropping one header by name leaves the rest. Turning this on is an explicit statement that the service's paging links are trusted to name other origins — it does send your default headers to them.

A relative nextLink is unaffected: HttpClient resolves it against BaseAddress, which makes it same-origin by construction.

var client = new OhDataClient(httpClient, new OhDataClientOptions
{
    FollowCrossOriginNextLinks = true   // only for a service you trust to page across origins
});

MaxNextLinkHops

The maximum number of @odata.nextLink hops a single enumeration may follow before failing with InvalidOperationException. Default 10_000.

while (nextLink != null) is driven entirely by the server: one that returns the same link forever makes the loop unbounded, and ToListAsync accumulates every page until the process runs out of memory. This is a termination guarantee, not a paging policy — the default is set high enough that no legitimate run reaches it (a million entities at a page size of 100). Values below 1 are rejected at configuration time; use int.MaxValue for effectively unlimited.

var client = new OhDataClient(httpClient, new OhDataClientOptions
{
    MaxNextLinkHops = 250_000
});

Next: Querying →