ListenItPro 1.0.2

dotnet add package ListenItPro --version 1.0.2
                    
NuGet\Install-Package ListenItPro -Version 1.0.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="ListenItPro" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ListenItPro" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="ListenItPro" />
                    
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 ListenItPro --version 1.0.2
                    
#r "nuget: ListenItPro, 1.0.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 ListenItPro@1.0.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=ListenItPro&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=ListenItPro&version=1.0.2
                    
Install as a Cake Tool

ListenItPro - Table Tracker Library

ListenItPro is a C# library designed to track changes (insert, update, delete) in a specified database table. It triggers events whenever changes occur, allowing you to respond to data modifications in real-time.

Installation

To install the package, run the following command:

dotnet add package ListenItPro

Enable Service Broker

Before you start tracking changes, ensure that Service Broker is enabled on your SQL Server database. Run the following commands:

ALTER DATABASE YourDatabaseName SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE;

<-- You can check if Service Broker is enabled by executing: -->

SELECT name, is_broker_enabled FROM sys.databases WHERE name = 'YourDatabaseName';

Usage

Create a subscriber class to listen to specific tables based on their names. For example, to listen to changes in the JobsQueue table, define a JobQueueSubscriber class like this:

using ListenItPro;
using System.Reflection;
using System.Text.Json;

namespace WebApplication1.Services
{
    public class JobsQueue
    {
        public int Id { get; set; } // Maps to the Id column (Primary Key)
        public string JobType { get; set; } // Maps to the JobType column
        public string JobData { get; set; } // Maps to the JobData column
        public string Status { get; set; } // Maps to the Status column (default 'Pending')
    }

    public class JobQueueSubscriber
    {
        private readonly SqlServerTableChangeListener _listener;

        public JobQueueSubscriber(SqlServerTableChangeListener listener)
        {
            _listener = listener;
            _listener.TableChanged += OnTableChanged;
        }

        // Called when a table change event occurs
        private void OnTableChanged(object tableData)
        {
            Console.WriteLine("Table data changed:");

            if (tableData is System.Dynamic.ExpandoObject expando)
            {
                var dict = (IDictionary<string, object>)expando;
                var jobque = _listener.MapToClass<JobsQueue>(dict);
                // Handle the changed data (e.g., update application state)
            }

            StartListening();
        }

        public void StartListening()
        {
            _listener.StartListeningForTable("JobsQueue", "Id");
        }

        public void StopListening()
        {
            _listener.StopListening();
        }
    }
}

Registering Services in Program.cs

Make sure to register your services in Program.cs to start listening for table changes:


builder.Services.AddSingleton<SqlServerTableChangeListener>(serviceProvider =>
    new SqlServerTableChangeListener(builder.Configuration));

builder.Services.AddSingleton<JobQueueSubscriber>(serviceProvider =>
    new JobQueueSubscriber(serviceProvider.GetRequiredService<SqlServerTableChangeListener>()));

var app = builder.Build();

var subscriber = app.Services.GetRequiredService<JobQueueSubscriber>();
subscriber.StartListening(); // Start listening for table changes

Contributing

Contributions are welcome! Please refer to CONTRIBUTING.md for guidelines on how to get involved.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

What do you think? Anything you’d like to tweak or add? Let’s make this README awesome! 😊

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 was computed.  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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 183 2/6/2025
1.0.1 184 2/5/2025 1.0.1 is deprecated because it is no longer maintained.
1.0.0 189 2/5/2025 1.0.0 is deprecated because it is no longer maintained.