ListenItPro 1.0.2
dotnet add package ListenItPro --version 1.0.2
NuGet\Install-Package ListenItPro -Version 1.0.2
<PackageReference Include="ListenItPro" Version="1.0.2" />
<PackageVersion Include="ListenItPro" Version="1.0.2" />
<PackageReference Include="ListenItPro" />
paket add ListenItPro --version 1.0.2
#r "nuget: ListenItPro, 1.0.2"
#:package ListenItPro@1.0.2
#addin nuget:?package=ListenItPro&version=1.0.2
#tool nuget:?package=ListenItPro&version=1.0.2
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 | Versions 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. |
-
net8.0
- Microsoft.Data.SqlClient (>= 6.0.1)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.