Skip to content
PapyrinePublic

About

A query framework to enable c# linq in Blazor Wasm apps backed by a EntityFramework backend.

Resources

Security policy

Stars

9 stars

Watchers

1 watching

Forks

Repository files navigation

Scry

Type-safe, serializable LINQ from a client to a server-side EF Core model.

When a UI changes quickly, server-side querying usually forces a choice. Either hand-code a bespoke endpoint and contract for each use case, or adopt GraphQL/OData and write queries in a separate query language. Scry removes that trade-off, and keeps everything in C# and strongly typed end to end:

  1. The EF Core model lives server-side. The client never references it — it is pointed at by path.
  2. A source generator in the client reads the model assembly directly by path (System.Reflection.Metadata), applies an allow-list, and generates strongly-typed client query DTOs plus a queryable entry point.
  3. The UI writes ordinary LINQ against the generated types.
  4. The LINQ is captured and serialized to a restricted query AST.
  5. The server deserializes, re-validates against the allow-list at runtime, rebinds to the real EF types, executes, and returns the projected rows.

To add or extend a query, write LINQ in the client. No new endpoint or contract is needed, and the server stays in full control of which types, properties, shapes, and rows can ever be returned.

Intended use

Scry is designed for a .NET client that runs outside the server's process — a Blazor WebAssembly front end, a WPF or Windows Forms desktop app, a console tool, or another service — that talks to its own back end. A TypeScript client covers a browser or Node front end in the same position. The client has no EF dependency, so it stays small in a trimmed WebAssembly publish and light in an installed desktop app, while still being strongly typed against the server's EF Core model. Client hosts covers what each host needs.

It also assumes the client and the back end are built by the same team and deployed together. A generated client is bound to the model surface it was generated against, and the two are expected to change in lockstep. Scry is deliberately not a general-purpose web API: it is not intended as a stable public contract for multiple external consumers, third-party apps, or clients on release cycles the team does not control. See docs/schema-versioning.md for how drift between the two is detected and mitigated.

"Same team" is about coupling, not trust. The client is still treated as hostile — the generated code, the LINQ, and the wire request are all attacker-controlled — and every guarantee is re-enforced server-side at runtime. See docs/security.md.

Compared to other approaches

Scry fills a narrow slot next to hand-written endpoints, schema-language query APIs, URL query conventions, expression-tree serializers, and database-generated APIs. Comparisons compares Scry with each of them, both by category and by named product, and lists when Scry is the wrong choice.

How it works

The build-time and runtime flows are deliberately independent. Nothing is referenced across the client/server boundary: the only things that cross it are the model dll by path (build time) and the serialized wire AST (run time).

Build time — generating the client

The source generator reads the EF model assembly by path and emits strongly-typed client query types from the allow-listed surface only. The assembly is never referenced, loaded, or executed.

flowchart TB
    subgraph model["Server model"]
        EF["EF model<br/>+ Scry.Annotations<br/>([Queryable], [QueryIgnore], …)"]
        DLL["Model dll"]
        EF --> DLL
    end

    subgraph client["Client (no EF dependency)"]
        GEN["Source generator<br/>reads dll via<br/>System.Reflection.Metadata"]
        GENTYPES["Generated query types<br/>(Scry.Generated)"]
        GEN --> GENTYPES
    end

    DLL -. "by path, never referenced" .-> GEN
Loading

Run time — a query round-trip

The client's LINQ is captured (never executed client-side) and serialized to a restricted AST. The server re-validates that AST against the allow-list, to completion and before anything is rebound. It then rebinds the AST to the real EF types, executes it, and returns only the projected rows. A byte[] member marked [BinaryTransfer] skips base64 and is sent as a raw multipart part next to the JSON (binary transfer). A member marked [Attachment] is not sent with the rows at all. The client fetches it on demand by row key, and the server authorizes each fetch with a separate check (attachments).

flowchart TB
    subgraph client["Client"]
        LINQ["UI writes linq<br/>against generated types"]
        CAPTURE["QueryProvider<br/>captures expression tree<br/>(never executed here)"]
        TRANS["QueryTranslator<br/>→ restricted query AST"]
        LINQ --> CAPTURE --> TRANS
    end

    subgraph wire["Scry.Wire"]
        REQ["QueryRequest AST<br/>(closed operator + node set)"]
    end

    subgraph server["Server"]
        SCHEMA["Schema.Build<br/>allow-list from the real model"]
        VALID["QueryValidator<br/>authoritative gate<br/>(runs to completion first)"]
        BUILD["ExpressionBuilder<br/>rebind to real EF types"]
        EXEC["QueryExecutor + ProjectionPlan<br/>execute + shape rows"]
        DB[("EF → DB")]
        RESP["QueryResponse"]
        SCHEMA -. "allow-list" .-> VALID
        VALID --> BUILD --> EXEC --> DB
        DB -- "projected rows" --> RESP
    end

    TRANS -- "serialize + send" --> REQ
    REQ -- "deserialize" --> VALID
    RESP -- "rows" --> LINQ
Loading

See docs/security.md for the full threat model.

Writes — commands

Queries never write. Writes are commands. A command is a class in the model marked [Command], generated into the client next to the query models and sent through Query.Commands. The server binds the payload into its own class and checks it against the command's policy, row by row for a command that targets a row. It then hands the command to an in-process handler or a message bus. If the command finishes within a second, the server returns the outcome at once. Otherwise it answers Pending and streams the outcome on the same response. The command's changes reach every screen through the live queries that read the changed data.

Packages

Package Purpose
Scry.Annotations Allow-list attributes applied to the server model.
Scry.Wire The serializable query AST shared by client and server.
Scry.Client Client-side IQueryable provider (no EF dependency). Ships the source generator.
Scry.Server Server-side validation + execution against EF Core.
Scry.Server.Explorer Opt-in, GraphiQL-style query explorer.
Scry.Describe A .NET tool, scry-describe, that writes the server's description from the model DLL at build time, for the TypeScript client.
Scry.Server.Delta Opt-in 304 Not Modified, and a change probe for live queries, backed by Delta.
Scry.Server.SignalR Opt-in: the query surface over a SignalR hub, so many live queries share one connection.
Scry.Client.SignalR Opt-in: a ScryClient over a SignalR hub connection.
Scry.Server.Mcp Opt-in: the query surface, and optionally commands, served to AI agents over MCP.
Scry.Server.Redis Opt-in: carries live-query change notifications between server nodes over Redis pub/sub.
Scry.Server.MessagePipe Opt-in: the same over MessagePipe's distributed pub/sub.
Scry.Server.NServiceBus Opt-in: the same over NServiceBus, including changes saved by a worker endpoint's handlers. Also carries commands to a worker, and answers each one when the worker replies.
Scry.Server.MassTransit Opt-in: commands carried over MassTransit.
Scry.Server.Rebus Opt-in: commands carried over Rebus.
Scry.Server.Wolverine Opt-in: commands carried over Wolverine.
Scry.Server.Disclosure.SqlServer Opt-in: stores the disclosure audit, a record of everything the server sent to each caller, in SQL Server.
Scry.Server.Disclosure.Explorer Opt-in: a browser explorer over that record, for anyone who has to find out who saw what.

Scry.SourceGenerator is packed inside Scry.Client rather than published separately.

Every package puts its public types in the single Scry namespace, so one using Scry; covers all of them. There are two exceptions. The generated query models are in Scry.Generated, and the client's async terminals (ToListAsync, FirstOrDefaultAsync, …) are in Scry.Linq. The generated client imports Scry.Linq itself. A project that references EF Core as well as Scry.Client only gets those terminals if it imports Scry.Linq explicitly.

At a glance

Annotate the server model:

[Queryable]
public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public Status Status { get; set; }
    public bool Active { get; set; }
    public DateOnly Created { get; set; }

    public int? ManagerId { get; set; }
    public Employee? Manager { get; set; }

    public int DepartmentId { get; set; }
    public Department? Department { get; set; }

    // A claim check rather than a value: no query reads it, and what a client gets back is a handle
    // carrying this row's key. A photo is the case the attribute exists for — bytes nothing wants on
    // every row of every query, fetched by the one thing that actually wants to draw them. The check
    // that authorizes the fetch is registered by the server; this project references the annotations
    // alone, so [AttachmentWith] has no policy type to name here.
    [Attachment(ContentType = "image/svg+xml")]
    public byte[]? Photo { get; set; }

    // Never exposed to clients.
    [QueryIgnore]
    public decimal Salary { get; set; }

    // The other half of that pair: queryable, but never in a URL and never in a cache. [QueryIgnore]
    // hides a member outright; [Sensitive] keeps it askable while refusing the two ways its value
    // escapes — a query comparing it against a constant travels as a body rather than a URL, where the
    // constant would land in every access log on the way, and a response projecting it is sent
    // no-store, where a cacheable one would be written to the caller's disk.
    [Sensitive]
    public string Password { get; set; } = "";
}

snippet source | anchor

Register and map on the server:

services
    .AddScry<SampleContext>(_ =>
    {
        // Holiday is a [QueryablePoco]: it has no table, so the server supplies its rows. Every
        // [QueryablePoco] type must be registered here or AddScry throws at startup.
        _.AddPocoSource(_ => Holiday.Seed());
        // Department.Handbook and Employee.Photo are [Attachment]s, and one exposed without a
        // check is a startup failure. Registered here rather than by [AttachmentWith] because
        // the model project references the annotations alone and has no server type to name.
        _.AddAttachmentPolicy<Department, HandbookPolicy>();
        _.AddAttachmentPolicy<Employee, PhotoPolicy>();
        _.MaxPageSize = 200;

        // A row policy whose decision is too slow to run per row in SQL, so it runs in C# and
        // the server remembers what it answered. Revision is what tells it a row has changed
        // and needs deciding again — see /docs/policies.md and the /permissions page.
        _.AddCachedPolicy<Order, long, RegionAccessPolicy>(_ => _.Revision);

        // Repeat a query while nothing has been written and the answer is a 304 rather than a
        // re-execution. Optional, and off until a freshness source says how to tell — see
        // /docs/caching.md.
        _.UseDeltaFreshness<SampleContext>();

        // What a cached response belongs to. This server has sources whose answers depend on
        // who asked — the row policy above, and Department.Handbook's attachment check — and
        // MapScry refuses to start without this. The sample has no sign-in, so the caller
        // half is a constant; a real app returns its tenant or its principal, and a client
        // signing in as someone else is then never handed the previous one's rows.
        //
        // The grants version is the other half, and is the part worth copying. A response
        // varies by what the caller is allowed to see, and QueryFreshness only watches the
        // database — so a grant changing outside it would move nothing, and a cache holding
        // the old rows would go on answering with rows the caller has since lost.
        _.CacheScope = _ => $"sample-{_.RequestServices.GetRequiredService<RegionGrants>().Version}";

        // Live queries: the /live pages. Off until a server says how many it will hold open,
        // which is also what maps the route — see /docs/live-queries.md.
        _.MaxSubscriptions = 100;

        // The interceptor above reports this server's own saves, at once and by entity. This
        // watches the database's change marker for everything it cannot see: a bulk update,
        // another node, a script run by hand.
        _.UseDeltaChanges<SampleContext>();

        // Commands: the /commands page and the /live pages' Reprice. Off until a server says
        // how many it will have in flight, which is also what maps the routes — see
        // /docs/commands.md.
        _.UseSampleCommands();

        // An AI agent's way in: the schema, queries and commands over MCP, at /mcp. Off
        // until a server says what an agent may do — see /docs/mcp.md.
        _.Mcp = ScryMcpAccess.ReadWrite;
    });

snippet source | anchor

AddPocoSource supplies the rows for a [QueryablePoco] type — see POCO sources.

app.MapScry("/api/query");

snippet source | anchor

Point the client at the model by path — no reference:

<!-- The server model, pointed at by path. NOT referenced. -->
<ScryModelDll>$(MSBuildThisFileDirectory)..\Sample.Model\bin\$(Configuration)\net11.0\Sample.Model.dll</ScryModelDll>

snippet source | anchor

Then write LINQ:

employees = await Query
    .Employee
    .Where(_ => _.Active)
    .OrderBy(_ => _.Name)
    .Select(_ => new EmployeeRow(_.Name, _.Status, _.Manager!.Name, _.Department!.Name))
    .ToListAsync();

snippet source | anchor

Then send a command and read its outcome:

static async Task<int> Reprice(ScryQuery query, int id)
{
    var outcome = await query.Commands.RepriceOrder(new() {Id = id});
    if (outcome.Status == ScryCommandStatus.Pending)
    {
        Console.WriteLine($"Order {id} is being repriced…");
        outcome = await outcome.Completion;
    }

    switch (outcome.Status)
    {
        case ScryCommandStatus.Completed:
            Console.WriteLine($"Order {id} repriced.");
            return 0;
        case ScryCommandStatus.Failed:
            await Console.Error.WriteLineAsync($"Order {id} was not repriced: {outcome.Error}");
            return 1;
        default:
            await Console.Error.WriteLineAsync($"Whether order {id} was repriced is unknown: {outcome.Error}");
            return 1;
    }
}

snippet source | anchor

Live queries

Any query can be made live. Call Live() instead of ToListAsync(), or use LiveCount(), LiveAny() or LiveFirstOrDefault(). The query returns its result now, and again whenever the result changes:

protected override void Start()
{
    // The LINQ is what it would be for ToListAsync. Live() in its place means the answer keeps
    // arriving: now, and again whenever the rows it reads change.
    rows = Query
        .Order
        .OrderBy(_ => _.Id)
        .Select(_ => new OrderRow(_.Id, _.Region, _.Amount))
        .Live()
        .Subscribe(
            answer =>
            {
                orders = answer;
                InvokeAsync(StateHasChanged);
            },
            Failed);

    // Any terminal can be live. This one is a second subscription of its own, and the server
    // sends it a number rather than the rows.
    counting = Query
        .Order
        .LiveCount()
        .Subscribe(
            answer =>
            {
                count = answer;
                InvokeAsync(StateHasChanged);
            },
            Failed);
}

// A subscription outlives nothing: leaving the page ends it, and the server is told.
protected override async ValueTask Stop()
{
    if (rows is not null)
    {
        await rows.DisposeAsync();
    }

    if (counting is not null)
    {
        await counting.DisposeAsync();
    }
}

snippet source | anchor

A live query can be read as a stream (await foreach), through a callback, or as a plain IObservable for Rx. Scry itself references no reactive library. Each new result comes from running the query again through the allow-list and the row policies. It is sent only if it differs from the previous one, so a change the caller is not allowed to see sends nothing. Live queries are off until a server sets MaxSubscriptions.

The sample's live page: a table of orders and a live count, with buttons that write to the server and a switch between the SSE and SignalR transports

A live query runs again when an EF SaveChanges interceptor, the host, or the database's own change marker signals a change, and a poll catches anything they miss. Opt-in packages carry changes between server nodes over Redis, MessagePipe or NServiceBus, and serve the whole query surface over a SignalR hub. See Live queries.

Commands

A write is a class in the model marked [Command]. The client sends it through the generated Query.Commands. The server binds it into its own class, checks it against the command's policy, and hands it to a handler:

/// <summary>Deletes one employee — an inactive one, by the sample's policy.</summary>
[Command(typeof(Employee))]
public class DeleteEmployee
{
    public int Id { get; set; }
}

/// <summary>Renames one employee. A name containing "slow" takes a while, to show a command going pending.</summary>
[Command(typeof(Employee))]
public class RenameEmployee
{
    public int Id { get; set; }

    [StringLength(100, MinimumLength = 1)]
    public string Name { get; set; } = "";
}

/// <summary>Deactivates or reactivates one employee: what makes a row deletable, and deletable again.</summary>
[Command(typeof(Employee))]
public class SetEmployeeActive
{
    public int Id { get; set; }
    public bool Active { get; set; }
}

/// <summary>Hires an employee, answering with the new row's id.</summary>
[Command(Result = typeof(EmployeeCreated))]
public class CreateEmployee
{
    [StringLength(100, MinimumLength = 1)]
    public string Name { get; set; } = "";

    public int DepartmentId { get; set; }
    public Status Status { get; set; }
}

public class EmployeeCreated
{
    public int Id { get; set; }
}

snippet source | anchor

A targeted command adds a Can{Command} member to its target's query model. The member is computed in the database from the command's policy, so a screen can enable a row's button from the row itself. Inside a live query, the member is computed again for every result. A command that takes longer than the server's sync window is answered Pending. The client follows it to the end on the same response, and lists it in a pending-work panel. The command's changes reach the screen through the live queries that read the changed data. Commands are off until a server sets MaxPendingCommands, and they can be carried to a worker over NServiceBus, MassTransit, Rebus or Wolverine. See Commands.

The sample's commands page: a live table of employees, each row with Deactivate, Rename and Delete buttons, only the inactive row's Delete enabled, and a form to hire

Query explorer

Scry.Server.Explorer contains an opt-in, GraphiQL-style query explorer. It runs Roslyn in the browser for real IntelliSense and diagnostics against the allow-listed schema, and shows exactly what is sent over the wire:

app.MapScryExplorer("/scry");

The Scry explorer: the schema pane, the LINQ, the wire request it translated to, and the rows the server returned

The explorer is off unless mapped, and Development-only by default. A query builder next to the editor shows the query as rows of columns, filters, sorts and paging. Changes made there are written back into the query, so a query can be built without typing it. See Query explorer.

A Blazor client can also use the debug sidecar. It opens over the running app (Alt+Q) and shows every Scry exchange the page has made: decoded requests, pretty-printed responses and headers. One click opens the explorer with the captured query already filled in.

The sidecar open over the sample app: the captured exchanges, queries and attachment fetches alike, and one query's decoded request, response, and headers

Disclosure audit

The server can keep a record of everything it sends. The disclosure audit is opt-in, and its store answers three questions: which callers received a row and which version of it, what a caller received in a range of time, and whether a caller ever received a member.

builder.Services.AddScry<SampleContext>(
    _ => _.UseSqlServerDisclosureAudit(connectionString));

Each response is recorded before it is sent. If the store does not accept the record, the response is not sent. Content is stored once, however often it is sent, and separately from the record of who received it. So a row's content can be erased while the record that it was sent is kept. Scry.Server.Disclosure.Explorer contains an explorer over the record, and reading the record through it is recorded too:

The disclosure explorer answering who received one employee's row: two callers, the members each answer sent with the sensitive one flagged, the version each was sent, and the reviewer who has since opened one of those answers

The audit is off until a server is given somewhere to write the record. That record is the most sensitive data the host keeps. See Disclosure audit.

Documentation

Icon

Ripple by Zach Bogart via The Noun Project

About

A query framework to enable c# linq in Blazor Wasm apps backed by a EntityFramework backend.

Resources

Security policy

Stars

9 stars

Watchers

1 watching

Forks

Contributors

Languages