Chatter.SqlChangeFeed 0.14.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Chatter.SqlChangeFeed --version 0.14.2
                    
NuGet\Install-Package Chatter.SqlChangeFeed -Version 0.14.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Chatter.SqlChangeFeed" Version="0.14.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Chatter.SqlChangeFeed" Version="0.14.2" />
                    
Directory.Packages.props
<PackageReference Include="Chatter.SqlChangeFeed" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Chatter.SqlChangeFeed --version 0.14.2
                    
#r "nuget: Chatter.SqlChangeFeed, 0.14.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Chatter.SqlChangeFeed@0.14.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Chatter.SqlChangeFeed&version=0.14.2
                    
Install as a Cake Addin
#tool nuget:?package=Chatter.SqlChangeFeed&version=0.14.2
                    
Install as a Cake Tool

<a name="chatter-sqlchangefeed"></a> Chatter.SqlChangeFeed

Emit strongly-typed notifications whenever rows in a watched SQL Server table are inserted, updated, or deleted.

Overview

Chatter.SqlChangeFeed turns row-level changes in a SQL Server table into a change feed of in-process messages, without polling. It provisions a SQL trigger on the watched table that publishes changes onto a SQL Server Service Broker queue; Chatter's Service Broker receiver picks those messages up and dispatches them to your handlers.

This package was originally named Table Watcher (Chatter.TableWatcher); it is now Chatter.SqlChangeFeed. The "watcher" terminology still appears throughout the domain language.

By default, each change is delivered to your code as one of three strongly-typed events:

  • RowInsertedEvent<TRowChangeData>
  • RowUpdatedEvent<TRowChangeData>
  • RowDeletedEvent<TRowChangeData>

Alternatively you can opt out of that fan-out and handle the raw ProcessChangeFeedCommand<TRowChangeData> (a batch of ChangeFeedItem<TRowChangeData>) yourself.

Installation

dotnet add package Chatter.SqlChangeFeed

This package builds on Chatter.CQRS and Chatter.MessageBrokers.SqlServiceBroker, which are pulled in transitively.

Getting Started

1. Register the change feed

AddSqlChangeFeed<TRowChangedData> is the primary entry point. It is an extension on IChatterBuilder, so chain it off your existing Chatter registration. TRowChangedData is a type implementing IMessage whose properties map to the columns of the watched row.

using Chatter.CQRS.DependencyInjection;
using Chatter.SqlChangeFeed.DependencyInjection;

public void ConfigureServices(IServiceCollection services)
{
    services.AddChatterCqrs(typeof(Startup).Assembly)
            .AddSqlChangeFeed<MyRow>(
                connectionString: Configuration.GetConnectionString("Chatter"),
                databaseName: "MyDatabase",   // optional; falls back to the connection string's Initial Catalog
                tableName: "MyTable",
                optionsBuilder: opts => opts
                    .WithSchema("dbo")
                    .WithTypesOfChangesToWatch(ChangeTypes.Insert | ChangeTypes.Update | ChangeTypes.Delete));
}

// The IMessage that maps to a row in MyTable
public class MyRow : Chatter.CQRS.IMessage
{
    public int Id { get; set; }
    public string Name { get; set; }
}

A non-generic overload, AddSqlChangeFeed(Type rowChangedDataType, ...), is available when the row type is only known at runtime.

2. Provision the SQL dependencies

The Trigger, Stored Procedures, and Service Broker objects are not created at registration time. Call UseChangeFeedSqlMigrationsAsync<TRowChangedData> on the IServiceProvider during startup to run the Change Feed Migration:

public static async Task ProvisionChangeFeedAsync(IHost host, CancellationToken token)
{
    await host.Services.UseChangeFeedSqlMigrationsAsync<MyRow>(token);
}

A blocking bridge, UseChangeFeedSqlMigrations<TRowChangedData>, is also available; it is now [Obsolete] at warning level — it still compiles and still works. It now runs the install on the thread pool instead of blocking the calling thread directly, so it no longer deadlocks on a host that carries a SynchronizationContext. Async remains the recommended, eventual-destination form; prefer it wherever the host lets you await. Non-generic overloads of both take the row type as a Type argument, for when it is only known at runtime.

Read Install Requirements before running this against a live database.

The IApplicationBuilder overloads are gone

The package no longer references Microsoft.AspNetCore.Http.Abstractions, and these four overloads were removed:

  • UseChangeFeedSqlMigrations<TRowChangedData>(this IApplicationBuilder, CancellationToken)
  • UseChangeFeedSqlMigrations(this IApplicationBuilder, Type, CancellationToken)
  • UseChangeFeedSqlMigrationsAsync<TRowChangedData>(this IApplicationBuilder, CancellationToken)
  • UseChangeFeedSqlMigrationsAsync(this IApplicationBuilder, Type, CancellationToken)

If one of those is now a compile error, reach the surviving IServiceProvider form through one property access:

// before
app.UseChangeFeedSqlMigrations<MyRow>();

// after - the surviving form is asynchronous, so await it from an async startup
// path; discarding the returned Task lets the app start before provisioning finishes
await app.ApplicationServices.UseChangeFeedSqlMigrationsAsync<MyRow>();

A caller with nowhere to await — a synchronous Configure(IApplicationBuilder app) — can still reach app.ApplicationServices.UseChangeFeedSqlMigrations<MyRow>(), which blocks until the migration completes: it is deprecated ([Obsolete], warning-level), not removed.

3. Handle the change notifications

With the default behavior (ProcessChangeFeedCommandViaChatter = true), implement IMessageHandler<T> for whichever events you care about:

using Chatter.CQRS;
using Chatter.CQRS.Context;
using Chatter.SqlChangeFeed;

public class MyRowChangeHandler :
    IMessageHandler<RowInsertedEvent<MyRow>>,
    IMessageHandler<RowUpdatedEvent<MyRow>>,
    IMessageHandler<RowDeletedEvent<MyRow>>
{
    public Task Handle(RowInsertedEvent<MyRow> message, IMessageHandlerContext context)
    {
        var inserted = message.Inserted;
        // ...
        return Task.CompletedTask;
    }

    public Task Handle(RowUpdatedEvent<MyRow> message, IMessageHandlerContext context)
    {
        var before = message.OldValue;
        var after = message.NewValue;
        // ...
        return Task.CompletedTask;
    }

    public Task Handle(RowDeletedEvent<MyRow> message, IMessageHandlerContext context)
    {
        var deleted = message.Deleted;
        // ...
        return Task.CompletedTask;
    }
}

Handling the raw change feed instead

If you call ProcessTableChangesManually() on the options builder, Chatter does not fan out to the row events. Instead, handle the batch command directly:

public class MyChangeFeedHandler : IMessageHandler<ProcessChangeFeedCommand<MyRow>>
{
    public Task Handle(ProcessChangeFeedCommand<MyRow> message, IMessageHandlerContext context)
    {
        foreach (ChangeFeedItem<MyRow> change in message.Changes)
        {
            // change.Inserted and/or change.Deleted are populated depending on the operation
        }
        return Task.CompletedTask;
    }
}

Install Requirements

The Change Feed Migration runs DDL against the consumer's own database. Read this before running it against a live one.

Supported platforms

Target Supported Why
SQL Server (on-premises, VM, container) Yes Full SQL Server Service Broker.
Azure SQL Managed Instance Yes Full SQL Server Service Broker.
Azure SQL Database No The engine has no Service Broker at all.

On Azure SQL Database (SERVERPROPERTY('EngineEdition') = 5) the install procedure refuses with a named error identifying the engine edition and the watched table, before creating any object — rather than failing obscurely part-way through.

Server floor: SQL Server 2016 SP1. The install and uninstall Stored Procedures are created with CREATE OR ALTER, so an upgraded package replaces a stale procedure body instead of silently keeping it. CREATE OR ALTER arrived in SQL Server 2016 SP1.

Privileges

The installing principal must already hold ALTER on the target database. Sysadmin is no longer required, and install no longer transfers database ownership: the ALTER AUTHORIZATION ON DATABASE::<db> TO [sa] statement earlier versions emitted alongside ENABLE_BROKER is gone. Reassigning ownership to sa silently widened the privileges of every EXECUTE AS OWNER module in the consumer's database.

Enabling Service Broker terminates other sessions

When the target database has Service Broker disabled, the migration issues:

ALTER DATABASE [YourDatabase] SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE;

WITH ROLLBACK IMMEDIATE rolls back other sessions' open transactions on that database and disconnects them. Without it the statement waits for every other session on the database to close, so a first install behind a connection pool blocks indefinitely with no timeout and no diagnostic. This applies to the first install only — the statement sits behind an is_broker_enabled = 0 guard, so once the broker is on it is skipped on every later run.

Watched-table preconditions

Before any Service Broker object is created, the install procedure refuses — with an error naming the cause and the watched table — when:

  • the watched table does not exist, or
  • the watched table has no PRIMARY KEY. The Change Feed Trigger joins INSERTED to DELETED on the primary key columns, so a table carrying only a UNIQUE constraint is refused.

Because every precondition is checked first, a refused install leaves no partially created queue, service, or Trigger behind.

Configuration

AddSqlChangeFeed takes the connection string, database, and table directly. Everything else is configured through the optional Action<SqlChangeFeedOptionsBuilder>, which produces a SqlChangeFeedOptions.

Core arguments / SqlChangeFeedOptions

Property Meaning
ConnectionString Connection to the SQL Server hosting the watched table.
DatabaseName Database containing the table. Optional; defaults to the connection string's Initial Catalog.
TableName The table to watch.
SchemaName Schema of the table. Defaults to dbo.
ChangeFeedTriggerTypes Which operations to watch (ChangeTypes.Insert \| Update \| Delete). Defaults to all three.
ChangeFeedQueueName Name of the backing Service Broker queue. Drives the installed topology: the Change Feed Migration creates this queue and the receiver reads it. Defaults to a Chatter-generated name based on the row type.
ChangeFeedDeadLetterServiceName Service Broker service to which dead-lettered messages are routed. Drives the installed topology: the Change Feed Migration creates this service. Defaults to a generated name.
ProcessChangeFeedCommandViaChatter When true (default), Chatter fans changes out as RowInserted/RowUpdated/RowDeleted events. When false, you handle ProcessChangeFeedCommand<T> directly.

SqlChangeFeedOptionsBuilder methods

Method Effect
WithNameOfDatabaseToWatch(string) Sets the database name.
WithSchema(string) Sets the table schema (default dbo).
WithTypesOfChangesToWatch(ChangeTypes) Restricts which operations raise notifications.
EmitRowChangeEvents() Fan out to the row-change events (default).
ProcessTableChangesManually() Deliver the raw ProcessChangeFeedCommand<T> instead.
WithChangeFeedQueueName(string) Overrides the Service Broker queue name — both the queue the Change Feed Migration creates and the queue the receiver reads.
WithChangeFeedDeadLetterServiceName(string) Overrides the dead-letter service name — both the dead-letter service the Change Feed Migration creates and the receiver's dead-letter path.
WithErrorQueueName(string) Sets the error queue path for the receiver.
WithTransactionMode(TransactionMode) Atomicity of the receiver (default FullAtomicityViaInfrastructure).
WithMaxReceiveAttempts(int) Max receive attempts before recovery action (default 10).
WithReceiverTimeoutInMilliseconds(int) Receiver wait timeout (default -1, unlimited).
WithConversationLifetimeInSeconds(int) Service Broker dialog lifetime.
EnableConversationEncryption() / DisableConversationEncryption() Toggle Service Broker dialog encryption (default disabled).
WithCompressedMessageBody() / WithUncompressedMessageBody() Toggle message-body compression (default compressed).
WithMessageBodyType(string) / WithApplicationJsonUtf16CharsetMessageBodyType() Set the message body content type (default application/json; charset=utf-16).

A change feed queue name must be a single unqualified identifier — no dot, no brackets. The two sides read the configured value differently and neither side can be worked around from the other. The Change Feed Migration installs it as one object inside the configured schema (CREATE QUEUE [schema].[<the configured value>]), and a bracket in the value is escaped as a literal name character, so WithChangeFeedQueueName("[my.queue]") installs a queue whose name really is [my.queue]. The receiver instead reads the value as a possibly schema-qualified path — dbo.MyQueue is read as schema.queue — and never qualifies it with the configured schema. So a value carrying a dot or a bracket makes the receiver poll an object the migration never created, and pre-bracketing does not fix it. This is not new in 0.14.2: before the receiver bracket-quoted the queue name it interpolated it raw, and T-SQL parses an unquoted a.b as schema.object in exactly the same way.

Configured names now reach the installed topology. Previously WithChangeFeedQueueName bound only the receiver while the Change Feed Migration provisioned a default-named queue, so a consumer who set a queue name got a receiver reading a queue the migration never created. WithChangeFeedDeadLetterServiceName was dropped before it reached anything: the public SqlChangeFeedOptions.ChangeFeedDeadLetterServiceName property was never assigned during Build(), so both the migration and the receiver fell back to the generated name. Both configured names now flow through to the objects the migration installs, so a consumer who already set either one will see the effective object name change on the next migration run. The conversation service name stays derived from the row type in every case; that derived service is created on the configured queue, and the Trigger routes to the service rather than to the queue.

A diverged topology is refused, not installed over. Because a configured name changes the objects the migration installs, a database still carrying the objects a previous configuration installed is a divergence the migration cannot detect by name: a SERVICE records its queue binding in sys.services.service_queue_id, and no name-existence guard reads that column. Before it creates or alters any Service Broker object, the install Stored Procedure now checks those bindings and refuses the run with a named error when the derived conversation service is bound to a queue other than the configured one, or when a service other than the configured dead-letter service is bound to this change feed's dead-letter queue. The refusal is non-destructive and does not repair the divergence — see Re-running the migration for the remedy.

Configured names must also be distinct within their catalog. AddSqlChangeFeed throws ChangeFeedObjectNameCollisionException when a configured name collides with another derived name in the same catalog namespace, compared case-insensitively to match SQL Server's default collation. WithChangeFeedQueueName is rejected when it equals the dead-letter queue name, the Change Feed Trigger name, or either Change Feed Stored Procedure name — queues, triggers, and stored procedures all share the schema-scoped sys.objects namespace. WithChangeFeedDeadLetterServiceName is rejected when it equals the derived conversation service name, with which it shares the database-scoped service catalog. A queue name and a service name may still be identical: different catalogs, no collision.

Known limitation — default names key on the row type's simple name. Every default name ChangeFeedObjectNames derives — the conversation queue and service, the dead-letter queue and service, the Change Feed Trigger, and the install and uninstall Change Feed Stored Procedures — comes from the row type's simple name and never its namespace, so two row types sharing a simple name in different namespaces derive identical installed object names. When both watched tables live in the same schema the install aborts on the duplicate Change Feed Trigger name — that failure is the collision guard, not a defect. Configuration is not a full escape hatch: WithChangeFeedQueueName and WithChangeFeedDeadLetterServiceName override only 2 of those 7 names, so the other 5 still collide. The resolution is to give colliding row types distinct simple names.

How It Works

When you call UseChangeFeedSqlMigrationsAsync<T>, SqlDependencyManager<T> runs a set of generated SQL scripts (via ISqlDependencyManager) against the target database. For each watched row type it provisions:

  1. Service Broker objects — enables Service Broker on the database if needed (ENABLE_BROKER WITH ROLLBACK IMMEDIATE; see Install Requirements) and creates the message type, contract, a conversation queue + service, and a dead-letter queue + service (InstallAndConfigureSqlServiceBroker). Names come from a single derivation, ChangeFeedObjectNames: the conversation queue and the dead-letter service honour ChangeFeedQueueName / ChangeFeedDeadLetterServiceName where configured, and every other name is derived from ChatterServiceBrokerConstants and the row type's simple name.
  2. A trigger on the watched table (CreateChangeFeedTrigger) — an AFTER INSERT/UPDATE/DELETE trigger (scoped to ChangeFeedTriggerTypes) that serializes the affected inserted/deleted rows and SENDs them onto a Service Broker conversation as a compressed message.
  3. Install / uninstall Stored Procedures (CreateInstallationProcedure, CreateUninstallProcedure) — both are emitted with CREATE OR ALTER, so re-running the migration replaces a stale procedure body rather than keeping it. The install procedure checks the preconditions, wires everything together, and creates or refreshes the Trigger; the uninstall procedure tears the Trigger, queues, services, and procedures back down.

At runtime the queue is drained by Chatter's SQL Service Broker receiver. A ChangeFeedReceiver<T> (a BrokeredMessageReceiver) deserializes each batch into a ProcessChangeFeedCommand<T> of ChangeFeedItem<T>. Each item carries an Inserted and/or Deleted snapshot, which the receiver uses to decide whether the change was an insert (inserted only), delete (deleted only), or update (both), dispatching the matching event — unless you chose ProcessTableChangesManually().

Provisioning is manual, not automatic. Registration (AddSqlChangeFeed) only wires up DI; the SQL objects are created only when the Change Feed Migration is invoked at startup.

Re-running the migration, and watched-table schema drift

Re-running the Change Feed Migration is safe while the installed topology still matches your configuration, and it is also the repair path for a watched table whose schema changed. The Service Broker objects keep their IF NOT EXISTS guards, the Stored Procedures are replaced in place via CREATE OR ALTER, and the Trigger is reconciled against the watched table's current columns:

  • On every run the install procedure re-derives the watched table's column set from INFORMATION_SCHEMA and hashes it into a fingerprint, which it embeds as a leading comment in the Trigger it creates.
  • Fingerprint matches the one the installed Trigger carries — nothing changes. The Trigger is left completely untouched; its object_id and modify_date do not move.
  • Fingerprint differs, or is absent (a Trigger installed by an earlier package version carries no marker) — the Trigger is dropped and recreated from the current column set.

Only a Trigger installed on the watched table is a refresh candidate. A same-named trigger on some other table is left alone, so the install fails loudly on the duplicate name rather than dropping an object the change feed does not own.

When the installed topology no longer matches the configuration, the re-run is refused. The IF NOT EXISTS guards above key on names, and a name says nothing about the queue a SERVICE is actually bound to. So when WithChangeFeedQueueName or WithChangeFeedDeadLetterServiceName changed since the last run — or the installed objects were renamed out from under the change feed — the install Stored Procedure refuses with a named error, before it creates or alters any Service Broker object, if either of these holds:

  • the derived conversation service is bound to a queue other than the configured conversation queue, or
  • a service other than the configured dead-letter service is bound to this change feed's dead-letter queue.

The refusal does not repair the divergence. Nothing is rebound, dropped, or renamed — dropping an orphaned queue would destroy the notifications still sitting on it — and because the gate runs ahead of every Service Broker statement, a refused run leaves no partially created object behind. The remedy is explicit: run the already-installed Chatter_UninstallChangeFeed_<TRowChangedData> Stored Procedure for that change feed, then re-run the migration. That handle survives a refusal by design — the uninstall procedure is regenerated only after an install succeeds, so a refused or failed run leaves the procedure that tears down your existing objects untouched.

The failure mode this replaces. The Trigger's SELECT column list is fixed at the moment the Trigger is created. Previously a re-run early-returned as soon as the Trigger existed, so dropping or renaming a watched column left the Trigger referencing a column that no longer exists — and because the Trigger fires inside the consumer's own INSERT/UPDATE/DELETE, it aborted their writes to the watched table until someone manually uninstalled and reinstalled the change feed. Re-running the migration now repairs that instead.

Header Propagation (including Trace Context)

Change-feed messages carry no headers at all. They originate from the SQL trigger this package provisions, which SENDs a DEFAULT-message-type message directly onto the Service Broker queue — there is no producer-side Chatter dispatch to stamp a Message Context, so there is nothing to propagate and nothing for a receiver to extract.

The consequence for the opt-in tracing added in ADR-0010: W3C trace context (traceparent / tracestate) does not flow into a change-feed message, so handling a row change starts a new trace rather than continuing the trace of whatever wrote the row.

This is inherent to the change feed's trigger origin and is a pre-existing property that affects all headers alike — it is not something tracing introduced. It compounds with the receive side: because the trigger sends the DEFAULT message type, the queue is drained through the SQL Service Broker receiver's DEFAULT path, which itself builds a fresh header dictionary (see Header Propagation in the SqlServiceBroker README). Both ends are pinned by conformance tests, so a change that accidentally alters either is visible.

Domain Language

See ../CONTEXT.md for the change-feed / Table Watcher glossary and relationships.

← All Chatter modules

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Chatter.SqlChangeFeed:

Package Downloads
Chatter.IntegrationEventRelay.ServiceBroker

SQL Service Broker change source for Chatter.IntegrationEventRelay: captures row changes with triggers and Chatter.SqlChangeFeed

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.16.1 147 10/17/2026
0.16.0 410 9/29/2026
0.15.1 116 9/24/2026
0.15.0 114 9/24/2026
0.14.2 121 9/12/2026
0.14.1 138 9/2/2026
0.14.0 119 9/2/2026
0.13.0 123 9/2/2026
0.12.0 134 9/1/2026
0.11.0 183 6/16/2026
0.9.1 156 6/16/2026
0.9.0 158 5/30/2026
0.8.4 535 3/3/2023
0.8.3 1,177 4/2/2022
0.8.2 626 4/1/2022
0.8.1 666 4/1/2022
0.8.0 651 4/1/2022
0.7.0 946 3/4/2022